避免使用gorm中的Where短语。

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

Avoid Where phrase with gorm

问题

我正在尝试更新表中的每条记录:

sqlDB.Table("task").Where("1=1").Update("status", 1)

我无法避免使用1=1的条件语句。这样做是正确的吗?

英文:

I'm trying to update every record in a table:

sqlDB.Table("task").Where("1=1").Update("status", 1)

And I can't avoid the 1=1 where condition. Is that the right way to do it?

答案1

得分: 1

是的。1=1 是一个被广泛认可的永远为真的 WHERE 子句。去试试吧。

英文:

Yes. 1=1 is a widely recognized always-true WHERE clause. Go for it.

答案2

得分: 0

根据https://gorm.io/docs/update.html#Block-Global-Updates的说明,以下代码应该可以工作:

db.Session(&gorm.Session{AllowGlobalUpdate: true}).Model(&User{}).Update("name", "jinzhu")
英文:

According to https://gorm.io/docs/update.html#Block-Global-Updates

This should work:

db.Session(&gorm.Session{AllowGlobalUpdate: true}).Model(&User{}).Update("name", "jinzhu")

huangapple
  • 本文由 发表于 2022年1月29日 20:17:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/70905257.html
匿名

发表评论

匿名网友

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

确定