环境
go 1.19.5 gorm v1.23.10 mysql 5.6
代码
由于实际业务代码过于复杂,此处抽象举例
- 我有两张表,结构:
type User struct {
Id int
Name string
GroupId int
}
func (User) Tablename() string {
return "user"
}
type Group struct {
Id int
Name string
}
func (Group) Tablename() string {
return "group"
}
- 我想要实现的SQL结果:
update `user` set name = 'n'
left join `group` on `user`.`group_id` = `group`.`id`
where `group`.`name` = 'g';
- 我现在的写法:
var user User
tx := db.Table(user.Tablename()).Joins("left join group on user.group_id = group.id")
tx.Where("group.name = ?", "g").Updates(&User{Name: "n"})
生成的SQL:
update `user` set name = 'n' where `group`.`name` = 'g';
报错信息: Unknown column 'group.name' in 'where clause'
问题:我不确定是不是我使用方式有问题,我在google没有搜到有效答案。
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: a631807682
does not support
Comment From: dablelv
我也遇到了同样的问题,貌似 GORM 尚不支持 UPDATE JOIN 操作。解决办法是使用 DB.Exec() 方法执行原生 SQL。