You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using clause.Not and clause.And to create query Where conditions and faulty simplification is applied:
NOT(col1 = val1 AND col2 = val2) gets turned into (col1 <> val1 AND col2 <> val2), which is wrong.
The right simplification is to turn the AND into an OR, by De Morgan's Laws, so (col1 <> OR val1 AND col2 <> val2)
This is a regression that was introduced into gorm in 1.25.6. Testing against 1.25.5 returns the correct condition and expected rows in the included tests (see gorm playground PR).
Test for completeness
funcTestGORM(t*testing.T) {
DB.Create(&User{Name: "jinzhu", Active: true})
DB.Create(&User{Name: "ezequiel", Active: true})
DB.Create(&User{Name: "jinzhu", Active: false})
DB.Create(&User{Name: "ezequiel", Active: false})
varresults []Userconds:=clause.Not(clause.And(
clause.Eq{
Column: clause.Column{Name: "name"},
Value: "jinzhu",
},
clause.Eq{
Column: clause.Column{Name: "active"},
Value: true,
},
))
// Query with `NOT(name = 'jinzhu' AND active = true)` should return last three records// By De Morgan's Law this is equivalent to `name != 'jinzhu' OR active != true`// but current version (since 1.25.6) transforms this to// `name != 'jinzhu' AND active != true` which returns only the last recorderr:=DB.
Model(&User{}).
Clauses(clause.Where{Exprs: []clause.Expression{conds}}).
Find(&results).
Erroriferr!=nil {
t.Errorf("Failed, got error: %v", err)
}
iflen(results) !=3 {
t.Errorf("Failed, expected 3 records, got %v", len(results))
}
for_, r:=rangeresults {
ifr.Name=="jinzhu"&&r.Active {
t.Errorf("Failed, unexpected record: %v", r)
}
}
conds=clause.Not(clause.Or(
clause.Eq{
Column: clause.Column{Name: "name"},
Value: "jinzhu",
},
clause.Eq{
Column: clause.Column{Name: "active"},
Value: true,
},
))
// Query with `NOT(name = 'jinzhu' OR active = true)` should return last recorderr=DB.
Model(&User{}).
Clauses(clause.Where{Exprs: []clause.Expression{conds}}).
Find(&results).
Erroriferr!=nil {
t.Errorf("Failed, got error: %v", err)
}
iflen(results) !=1 {
t.Errorf("Failed, expected 1 records, got %v", len(results))
}
for_, r:=rangeresults {
ifr.Name=="jinzhu"||r.Active {
t.Errorf("Failed, unexpected record: %v", r)
}
}
The text was updated successfully, but these errors were encountered:
GORM Playground Link
go-gorm/playground#767
Description
When using clause.Not and clause.And to create query Where conditions and faulty simplification is applied:
NOT(col1 = val1 AND col2 = val2) gets turned into (col1 <> val1 AND col2 <> val2), which is wrong.
The right simplification is to turn the AND into an OR, by De Morgan's Laws, so (col1 <> OR val1 AND col2 <> val2)
This is a regression that was introduced into gorm in 1.25.6. Testing against 1.25.5 returns the correct condition and expected rows in the included tests (see gorm playground PR).
Test for completeness
The text was updated successfully, but these errors were encountered: