GORM Playground Link
https://github.com/go-gorm/playground/pull/632
Description
Group conditions is not working as expected after calling db.Model
sql1 := DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
return tx.Where(
tx.Where("pizza = ?", "pepperoni").Where(tx.Where("size = ?", "small").Or("size = ?", "medium")),
).Or(
tx.Where("pizza = ?", "hawaiian").Where("size = ?", "xlarge"),
).Find(&User{})
})
sql2 := DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
+ tx = tx.Model(&User{}) // use db.Model!!!
return tx.Where(
tx.Where("pizza = ?", "pepperoni").Where(tx.Where("size = ?", "small").Or("size = ?", "medium")),
).Or(
tx.Where("pizza = ?", "hawaiian").Where("size = ?", "xlarge"),
).Find(&User{})
})
These two sqls are expected to be the same. Actually sql2 is the following strange value.
SELECT * FROM `users` WHERE ((pizza = "pepperoni" AND size = "small" OR size = "medium") AND pizza = "hawaiian" AND size = "xlarge") AND `users`.`deleted_at` IS NULL
Why is it like this?