Skip to content

Commit

Permalink
only check references for updated columns
Browse files Browse the repository at this point in the history
  • Loading branch information
James Cor committed Oct 9, 2024
1 parent 487cf93 commit bc309d4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
35 changes: 35 additions & 0 deletions enginetest/queries/foreign_key_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -2581,6 +2581,41 @@ var ForeignKeyTests = []ScriptTest{
},
},
},
{
// https://github.com/dolthub/dolt/issues/7857
Name: "partial foreign key update",
SetUpScript: []string{
"create table parent1 (i int primary key);",
"create table child1 (" +
"i int primary key, " +
"j int, " +
"k int, " +
"index(j), " +
"index(k), " +
"foreign key (j) references parent1 (i)," +
"foreign key (k) references parent1 (i));",
"insert into parent1 values (1);",
"insert into child1 values (100, 1, 1);",
"set foreign_key_checks = 0;",
"insert into child1 values (101, 2, 2);",
"set foreign_key_checks = 1;",
},
Assertions: []ScriptTestAssertion{
{
Query: "update child1 set j = 1 where i = 101;",
Expected: []sql.Row{
{types.OkResult{RowsAffected: 1, Info: plan.UpdateInfo{Matched: 1, Updated: 1}}},
},
},
{
Query: "select * from child1",
Expected: []sql.Row{
{100, 1, 1},
{101, 1, 2},
},
},
},
},
}

var CreateForeignKeyTests = []ScriptTest{
Expand Down
11 changes: 11 additions & 0 deletions sql/plan/foreign_key_editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ func (fkEditor *ForeignKeyEditor) IsInitialized(editors map[*ForeignKeyEditor]st
// Update handles both the standard UPDATE statement and propagated referential actions from a parent table's ON UPDATE.
func (fkEditor *ForeignKeyEditor) Update(ctx *sql.Context, old sql.Row, new sql.Row, depth int) error {
for _, reference := range fkEditor.References {
// Only check the reference for the columns that are updated
hasChange := false
for _, idx := range reference.RowMapper.IndexPositions {
if old[idx] != new[idx] {
hasChange = true
break
}
}
if !hasChange {
continue
}
if err := reference.CheckReference(ctx, new); err != nil {
return err
}
Expand Down

0 comments on commit bc309d4

Please sign in to comment.