mirror of
https://github.com/ent/ent.git
synced 2026-04-29 22:20:54 +03:00
entc/gen: move sql-update-one to sqlgraph
This commit is contained in:
@@ -630,15 +630,28 @@ func (u *updater) setExternalEdges(ctx context.Context, ids []driver.Value, addE
|
||||
|
||||
// setTableColumns sets the table columns and foreign_keys used in insert.
|
||||
func (u *updater) setTableColumns(update *sql.UpdateBuilder, addEdges, clearEdges map[Rel][]*EdgeSpec) error {
|
||||
// Avoid multiple assignments to same column.
|
||||
setEdges := make(map[string]bool)
|
||||
for _, e := range addEdges[M2O] {
|
||||
setEdges[e.Columns[0]] = true
|
||||
}
|
||||
for _, e := range addEdges[O2O] {
|
||||
if e.Inverse || e.Bidi {
|
||||
setEdges[e.Columns[0]] = true
|
||||
}
|
||||
}
|
||||
for _, fi := range u.Fields.Clear {
|
||||
update.SetNull(fi.Column)
|
||||
}
|
||||
for _, e := range clearEdges[M2O] {
|
||||
update.SetNull(e.Columns[0])
|
||||
if col := e.Columns[0]; !setEdges[col] {
|
||||
update.SetNull(col)
|
||||
}
|
||||
}
|
||||
for _, e := range clearEdges[O2O] {
|
||||
if e.Inverse || e.Bidi {
|
||||
update.SetNull(e.Columns[0])
|
||||
col := e.Columns[0]
|
||||
if (e.Inverse || e.Bidi) && !setEdges[col] {
|
||||
update.SetNull(col)
|
||||
}
|
||||
}
|
||||
err := setTableColumns(u.Fields.Set, addEdges, func(column string, value driver.Value) {
|
||||
|
||||
@@ -885,8 +885,9 @@ func TestUpdateNode(t *testing.T) {
|
||||
},
|
||||
prepare: func(mock sqlmock.Sqlmock) {
|
||||
mock.ExpectBegin()
|
||||
// Clear the "partner" and "spouse 2" from 1's column, and set "spouse 3".
|
||||
mock.ExpectExec(escape("UPDATE `users` SET `partner_id` = NULL, `spouse_id` = NULL, `spouse_id` = ? WHERE `id` = ?")).
|
||||
// Clear the "partner" from 1's column, and set "spouse 3".
|
||||
// "spouse 2" is implicitly removed when setting a different foreign-key.
|
||||
mock.ExpectExec(escape("UPDATE `users` SET `partner_id` = NULL, `spouse_id` = ? WHERE `id` = ?")).
|
||||
WithArgs(3, 1).
|
||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
// Clear the "partner_id" column from previous 1's partner.
|
||||
|
||||
Reference in New Issue
Block a user