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

This commit is contained in:
胡标 2023-01-16 15:05:34 +08:00
parent 697839e94d
commit 44da32cf6a

View File

@ -139,8 +139,14 @@ func (db *DB) First(dest interface{}, conds ...interface{}) (tx *DB) {
func (db *DB) Take(dest interface{}, conds ...interface{}) (tx *DB) { func (db *DB) Take(dest interface{}, conds ...interface{}) (tx *DB) {
tx = db.Limit(1) tx = db.Limit(1)
if len(conds) > 0 { if len(conds) > 0 {
if exprs := tx.Statement.BuildCondition(conds[0], conds[1:]...); len(exprs) > 0 { pkValue, ok := conds[0].(string)
tx.Statement.AddClause(clause.Where{Exprs: exprs}) if len(conds) == 1 && ok {
cond := []clause.Expression{clause.IN{Column: clause.PrimaryColumn, Values: []interface{}{pkValue}}}
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
@ -155,8 +161,14 @@ func (db *DB) Last(dest interface{}, conds ...interface{}) (tx *DB) {
Desc: true, Desc: true,
}) })
if len(conds) > 0 { if len(conds) > 0 {
if exprs := tx.Statement.BuildCondition(conds[0], conds[1:]...); len(exprs) > 0 { pkValue, ok := conds[0].(string)
tx.Statement.AddClause(clause.Where{Exprs: exprs}) if len(conds) == 1 && ok {
cond := []clause.Expression{clause.IN{Column: clause.PrimaryColumn, Values: []interface{}{pkValue}}}
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