fix(statement): The primary key type is not int, but string or uuid (#5984)

This commit is contained in:
胡标 2023-01-16 13:56:23 +08:00
parent 694cd49df5
commit 4df6056d9b
2 changed files with 14 additions and 9 deletions

View File

@ -120,8 +120,13 @@ func (db *DB) First(dest interface{}, conds ...interface{}) (tx *DB) {
Column: clause.Column{Table: clause.CurrentTable, Name: clause.PrimaryKey}, Column: clause.Column{Table: clause.CurrentTable, Name: clause.PrimaryKey},
}) })
if len(conds) > 0 { if len(conds) > 0 {
if exprs := tx.Statement.BuildCondition(conds[0], conds[1:]...); len(exprs) > 0 { if len(conds) == 1 {
tx.Statement.AddClause(clause.Where{Exprs: exprs}) cond := []clause.Expression{clause.IN{Column: clause.PrimaryColumn, Values: []interface{}{conds[0]}}}
tx.Statement.AddClause(clause.Where{Exprs: cond})
} else {
if exprs := tx.Statement.BuildCondition(conds[0], conds[1:]...); len(exprs) > 0 {
tx.Statement.AddClause(clause.Where{Exprs: exprs})
}
} }
} }
tx.Statement.RaiseErrorOnNotFound = true tx.Statement.RaiseErrorOnNotFound = true
@ -427,8 +432,13 @@ func (db *DB) UpdateColumns(values interface{}) (tx *DB) {
func (db *DB) Delete(value interface{}, conds ...interface{}) (tx *DB) { func (db *DB) Delete(value interface{}, conds ...interface{}) (tx *DB) {
tx = db.getInstance() tx = db.getInstance()
if len(conds) > 0 { if len(conds) > 0 {
if exprs := tx.Statement.BuildCondition(conds[0], conds[1:]...); len(exprs) > 0 { if len(conds) == 1 {
tx.Statement.AddClause(clause.Where{Exprs: exprs}) cond := []clause.Expression{clause.IN{Column: clause.PrimaryColumn, Values: []interface{}{conds[0]}}}
tx.Statement.AddClause(clause.Where{Exprs: cond})
} else {
if exprs := tx.Statement.BuildCondition(conds[0], conds[1:]...); len(exprs) > 0 {
tx.Statement.AddClause(clause.Where{Exprs: exprs})
}
} }
} }
tx.Statement.Dest = value tx.Statement.Dest = value

View File

@ -287,11 +287,6 @@ func (stmt *Statement) BuildCondition(query interface{}, args ...interface{}) []
return nil return nil
} }
if len(args) == 0 && (!strings.Contains(s, "?") && !strings.Contains(s, "@")) {
// The primary key type is not int, but string or uuid (#5984)
return []clause.Expression{clause.IN{Column: clause.PrimaryColumn, Values: []interface{}{s}}}
}
if len(args) == 0 || (len(args) > 0 && strings.Contains(s, "?")) { if len(args) == 0 || (len(args) > 0 && strings.Contains(s, "?")) {
// looks like a where condition // looks like a where condition
return []clause.Expression{clause.Expr{SQL: s, Vars: args}} return []clause.Expression{clause.Expr{SQL: s, Vars: args}}