计算所选字段在响应中不存在

huangapple go评论91阅读模式
英文:

computed selected field not present in response

问题

我有一个在gorm中的模型,其中有两个字段,我不希望它们出现在表中,因为它们是通过SQL查询计算出来的:

type User struct {
    ID            uint        `json:"id" gorm:"primarykey,autoIncrement"`
    MutedUsers    []*User     `json:"-" gorm:"many2many:user_muted_users;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
    BlockedUsers  []*User     `json:"-" gorm:"many2many:user_blocked_users;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
    ...
    IsMuted       bool        `json:"is_muted" gorm:"-"`
    IsBlocked     bool        `json:"is_blocked" gorm:"-"`
}

计算上述字段的查询:

var users []models.User

userMutedQ := database.Instance.
    Table("user_muted_users").
    Select("1").
    Where("user_id = ? and muted_user_id = u.id", 1)

userBlockedQ := database.Instance.
    Table("user_blocked_users").
    Select("1").
    Where("user_id = ? and blocked_user_id = u.id", 1)

database.Instance.
    Table("users u").
    Select("u.*, "+
        "(case when exists(?) then 'true' else 'false' end) as is_muted, "+
        "(case when exists(?) then 'true' else 'false' end) as is_blocked, ",
        userMutedQ, userBlockedQ, 
    ).
    Where("u.id = 1").
    Scan(&users)

在数据库中运行时,控制台上的SQL将生成具有正确值的is_mutedis_blocked列(一些为true,一些为false)。

然而,users列表中的所有is_mutedis_blocked的值都为false。也许是因为gorm由于-标志定义而忽略了它们。如果我不使用-,那么gorm将在我的数据库中创建完全多余的is_mutedis_blocked列,因为这些值总是计算得出的。

这里应该怎么做呢?

英文:

I have this model in gorm with 2 fields which I do not want inside the table as they are computed from the sql query:

type User struct {
	ID          uint        `json:"id" gorm:"primarykey,autoIncrement"`
    MutedUsers     []*User `json:"-" gorm:"many2many:user_muted_users;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
    BlockedUsers   []*User `json:"-" gorm:"many2many:user_blocked_users;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
    ...

	IsMuted     bool `json:"is_muted" gorm:"-"`
	IsBlocked   bool `json:"is_blocked" gorm:"-"`
}

Query which computes above fields:

var users []models.User

userMutedQ := database.Instance.
	Table("user_muted_users").
	Select("1").
	Where("user_id = ? and muted_user_id = u.id", 1)

userBlockedQ := database.Instance.
	Table("user_blocked_users").
	Select("1").
	Where("user_id = ? and blocked_user_id = u.id", 1)

database.Instance.
	Table("users u").
	Select("u.*, "+
		"(case when exists(?) then 'true' else 'false' end) as is_muted, "+
		"(case when exists(?) then 'true' else 'false' end) as is_blocked, ",
		userMutedQ, userBlockedQ, 
	).
	Where("u.id = 1").
	Scan(&users)

The sql from the console, when run against the database will produce columns with the right value for is_muted is_blocked (some true some false).

The users list however, have all values for is_muted is_blocked to false. Maybe gorm is ignoring them due to the - flag definition. If I don't use - then gorm will create is_muted is_blocked columns inside my database which are totally redundant since the value is always computed.

What is the right way here?

答案1

得分: 0

根据文档新功能添加字段标签以忽略迁移PR),你可以使用migration指令来使用-标签在迁移期间忽略字段创建,并使用->来设置字段的只读权限:

IsBlocked bool `gorm:"-;migration"`
英文:

According to docs (Declaring Models/Field-Level Permission) and new feature (Add field tag to ignore migration and PR), you can use migration directive for - tag to ignore field creation during migration and use -> to readonly field permission:

IsBlocked bool `gorm:"->;-:migration"`

huangapple
  • 本文由 发表于 2021年9月12日 13:12:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/69148639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定