简体   繁体   English

避免使用 gorm 的 Where 短语

[英]Avoid Where phrase with gorm

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.而且我无法避免1=1 where 条件。 Is that the right way to do it?这是正确的方法吗?

Yes.是的。 1=1 is a widely recognized always-true WHERE clause. 1=1是一个被广泛认可的永远正确的 WHERE 子句。 Go for it. Go 为它。

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

This should work:这应该工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM