fix(statement): The primary key type is not int, but string or uuid (#5984)
This commit is contained in:
parent
694cd49df5
commit
4df6056d9b
@ -120,10 +120,15 @@ 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 len(conds) == 1 {
|
||||||
|
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 {
|
if exprs := tx.Statement.BuildCondition(conds[0], conds[1:]...); len(exprs) > 0 {
|
||||||
tx.Statement.AddClause(clause.Where{Exprs: exprs})
|
tx.Statement.AddClause(clause.Where{Exprs: exprs})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
tx.Statement.RaiseErrorOnNotFound = true
|
tx.Statement.RaiseErrorOnNotFound = true
|
||||||
tx.Statement.Dest = dest
|
tx.Statement.Dest = dest
|
||||||
return tx.callbacks.Query().Execute(tx)
|
return tx.callbacks.Query().Execute(tx)
|
||||||
@ -427,10 +432,15 @@ 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 len(conds) == 1 {
|
||||||
|
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 {
|
if exprs := tx.Statement.BuildCondition(conds[0], conds[1:]...); len(exprs) > 0 {
|
||||||
tx.Statement.AddClause(clause.Where{Exprs: exprs})
|
tx.Statement.AddClause(clause.Where{Exprs: exprs})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
tx.Statement.Dest = value
|
tx.Statement.Dest = value
|
||||||
return tx.callbacks.Delete().Execute(tx)
|
return tx.callbacks.Delete().Execute(tx)
|
||||||
}
|
}
|
||||||
|
@ -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}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user