gorm icon indicating copy to clipboard operation
gorm copied to clipboard

chained .Where().Where() clauses grouping incorrect

Open jhartman86 opened this issue 2 years ago • 3 comments

The way GORM applies () grouping mechanisms around where clauses has changed between minor versions (I can say specifically in our case, v1.22.5 -> 1.23.8), resulting in drastically different meanings of the WHERE clauses.

Previously (v1.22.5):

gorm.Where('a=b OR c=d').Where('x=y').Find(&thing)
// the sql would be: SELECT * FROM thing WHERE (a=b OR c=d) AND (x=y)

In v1.23.8:

gorm.Where('a=b OR c=d').Where('x=y').Find(&thing)
// the sql would be: SELECT * FROM thing WHERE a=b OR c=d AND x=y

In short, changing GORM's grouping behavior in the where clauses has drastic side effects, and is absolutely a breaking change.


For clarification, we are using .Scopes() to decorate GORM w/ Where clauses, if that's at all relevant.


Is this a documented change, or was this a planned change?

Thanks in advance and let me know if more details are helpful.

jhartman86 avatar Sep 09 '22 19:09 jhartman86

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

github-actions[bot] avatar Sep 09 '22 20:09 github-actions[bot]

I didn't find this problem in the last version, please provide a minimal reproduction.

DB.Where("name = ? or name = ?", "jinzhu", "").Where("age = 0").Find(&result)
SELECT * FROM `users` WHERE (name = 'jinzhu' or name = '') AND age = 0 AND `users`.`deleted_at` IS NULL

a631807682 avatar Sep 20 '22 09:09 a631807682

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

github-actions[bot] avatar Sep 20 '22 09:09 github-actions[bot]