英文:
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_muted和is_blocked列(一些为true,一些为false)。
然而,users列表中的所有is_muted和is_blocked的值都为false。也许是因为gorm由于-标志定义而忽略了它们。如果我不使用-,那么gorm将在我的数据库中创建完全多余的is_muted和is_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"`
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论