Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor: fix issue of insert into parent table failed cause by foreign key check #39201

Merged
merged 4 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion executor/fktest/foreign_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ func TestForeignKeyOnInsertOnDuplicateParentTableCheck(t *testing.T) {
tk.MustQuery("select id, a, b, name from t2 order by id").Check(testkit.Rows("1 11 21 a"))

tk.MustExec("insert into t1 (id, a, b) values (1, 11, 21) on duplicate key update id=11")
tk.MustGetDBError("insert into t1 (id, a, b) values (1, 11, 21) on duplicate key update a=a+10, b=b+20", plannercore.ErrRowIsReferenced2)
tk.MustGetDBError("insert into t1 (id, a, b) values (11, 11, 21) on duplicate key update a=a+10, b=b+20", plannercore.ErrRowIsReferenced2)
tk.MustQuery("select id, a, b from t1 order by id").Check(testkit.Rows("2 1112 2222", "3 1013 2023", "4 14 24", "11 11 21"))
tk.MustQuery("select id, a, b, name from t2 order by id").Check(testkit.Rows("1 11 21 a"))
}
Expand All @@ -567,6 +567,15 @@ func TestForeignKeyOnInsertOnDuplicateParentTableCheck(t *testing.T) {
tk.MustGetDBError("insert into t1 (id, a, b) values (1, 0, 0) on duplicate key update id=100+id", plannercore.ErrRowIsReferenced2)
tk.MustQuery("select id, a, b from t1 order by id").Check(testkit.Rows("1 111 21", "4 14 24", "102 12 22", "103 13 23"))
tk.MustQuery("select id, a, b, name from t2 order by id").Check(testkit.Rows("11 1 21 a"))

// Case-10: Test insert into parent table failed cause by foreign key check, see https://github.com/pingcap/tidb/issues/39200.
tk.MustExec("drop table if exists t1,t2;")
tk.MustExec("create table t1 (id int key);")
tk.MustExec("create table t2 (id int, foreign key fk(id) references t1(id));")
tk.MustExec("set @@foreign_key_checks=0")
tk.MustExec("insert into t2 values (1)")
tk.MustExec("set @@foreign_key_checks=1")
tk.MustExec("insert into t1 values (1) on duplicate key update id=2")
}

func TestForeignKey(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions executor/foreign_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ func buildFKCheckExec(sctx sessionctx.Context, tbl table.Table, fkCheck *planner
}

func (fkc *FKCheckExec) insertRowNeedToCheck(sc *stmtctx.StatementContext, row []types.Datum) error {
if fkc.ReferredFK != nil {
// Insert into parent table doesn't need to do foreign key check.
return nil
}
return fkc.addRowNeedToCheck(sc, row)
}

Expand Down