Skip to content

Commit bc309d4

Browse files
author
James Cor
committed
only check references for updated columns
1 parent 487cf93 commit bc309d4

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

enginetest/queries/foreign_key_queries.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,6 +2581,41 @@ var ForeignKeyTests = []ScriptTest{
25812581
},
25822582
},
25832583
},
2584+
{
2585+
// https://github.com/dolthub/dolt/issues/7857
2586+
Name: "partial foreign key update",
2587+
SetUpScript: []string{
2588+
"create table parent1 (i int primary key);",
2589+
"create table child1 (" +
2590+
"i int primary key, " +
2591+
"j int, " +
2592+
"k int, " +
2593+
"index(j), " +
2594+
"index(k), " +
2595+
"foreign key (j) references parent1 (i)," +
2596+
"foreign key (k) references parent1 (i));",
2597+
"insert into parent1 values (1);",
2598+
"insert into child1 values (100, 1, 1);",
2599+
"set foreign_key_checks = 0;",
2600+
"insert into child1 values (101, 2, 2);",
2601+
"set foreign_key_checks = 1;",
2602+
},
2603+
Assertions: []ScriptTestAssertion{
2604+
{
2605+
Query: "update child1 set j = 1 where i = 101;",
2606+
Expected: []sql.Row{
2607+
{types.OkResult{RowsAffected: 1, Info: plan.UpdateInfo{Matched: 1, Updated: 1}}},
2608+
},
2609+
},
2610+
{
2611+
Query: "select * from child1",
2612+
Expected: []sql.Row{
2613+
{100, 1, 1},
2614+
{101, 1, 2},
2615+
},
2616+
},
2617+
},
2618+
},
25842619
}
25852620

25862621
var CreateForeignKeyTests = []ScriptTest{

sql/plan/foreign_key_editor.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ func (fkEditor *ForeignKeyEditor) IsInitialized(editors map[*ForeignKeyEditor]st
8181
// Update handles both the standard UPDATE statement and propagated referential actions from a parent table's ON UPDATE.
8282
func (fkEditor *ForeignKeyEditor) Update(ctx *sql.Context, old sql.Row, new sql.Row, depth int) error {
8383
for _, reference := range fkEditor.References {
84+
// Only check the reference for the columns that are updated
85+
hasChange := false
86+
for _, idx := range reference.RowMapper.IndexPositions {
87+
if old[idx] != new[idx] {
88+
hasChange = true
89+
break
90+
}
91+
}
92+
if !hasChange {
93+
continue
94+
}
8495
if err := reference.CheckReference(ctx, new); err != nil {
8596
return err
8697
}

0 commit comments

Comments
 (0)