diff --git a/ddl/cancel_test.go b/ddl/cancel_test.go index 3ca0f39d877a5..3a5c461ad8461 100644 --- a/ddl/cancel_test.go +++ b/ddl/cancel_test.go @@ -150,7 +150,7 @@ var allTestCase = []testCancelJob{ {"alter table t modify column c11 char(10)", false, model.StatePublic, false, true, nil}, // Add foreign key. {"alter table t add constraint fk foreign key a(c1) references t_ref(c1)", true, model.StateNone, true, false, []string{"create table t_ref (c1 int key, c2 int, c3 int, c11 tinyint);"}}, - {"alter table t add constraint fk foreign key a(c1) references t_ref(c1)", false, model.StatePublic, false, true, nil}, + {"alter table t add constraint fk foreign key a(c1) references t_ref(c1)", false, model.StatePublic, false, true, []string{"insert into t_ref (c1) select c1 from t;"}}, // Drop foreign key. {"alter table t drop foreign key fk", true, model.StatePublic, true, false, nil}, {"alter table t drop foreign key fk", false, model.StateNone, false, true, nil}, diff --git a/ddl/column_type_change_test.go b/ddl/column_type_change_test.go index 818d0714080f1..4f79ce7782368 100644 --- a/ddl/column_type_change_test.go +++ b/ddl/column_type_change_test.go @@ -1812,8 +1812,7 @@ func TestChangingAttributeOfColumnWithFK(t *testing.T) { tk.MustExec("use test") prepare := func() { - tk.MustExec("drop table if exists users") - tk.MustExec("drop table if exists orders") + tk.MustExec("drop table if exists users, orders") tk.MustExec("CREATE TABLE users (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, doc JSON);") tk.MustExec("CREATE TABLE orders (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, user_id INT NOT NULL, doc JSON, FOREIGN KEY fk_user_id (user_id) REFERENCES users(id));") } diff --git a/ddl/db_foreign_key_test.go b/ddl/db_foreign_key_test.go index dc60b53112291..f45db3934d89a 100644 --- a/ddl/db_foreign_key_test.go +++ b/ddl/db_foreign_key_test.go @@ -26,8 +26,7 @@ func TestDuplicateForeignKey(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("drop table if exists t1") + tk.MustExec("drop table if exists t, t1") // Foreign table. tk.MustExec("create table t(id int key)") // Create target table with duplicate fk. @@ -38,8 +37,7 @@ func TestDuplicateForeignKey(t *testing.T) { // Alter target table with duplicate fk. tk.MustGetErrCode("alter table t1 add CONSTRAINT `fk_aaa` FOREIGN KEY (`id_fk`) REFERENCES `t` (`id`)", mysql.ErrFkDupName) tk.MustGetErrCode("alter table t1 add CONSTRAINT `fk_aAa` FOREIGN KEY (`id_fk`) REFERENCES `t` (`id`)", mysql.ErrFkDupName) - tk.MustExec("drop table if exists t") - tk.MustExec("drop table if exists t1") + tk.MustExec("drop table if exists t, t1") } func TestTemporaryTableForeignKey(t *testing.T) { diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index 4b2468b3be9c4..442b797a54dbc 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -2378,6 +2378,7 @@ func TestParserIssue284(t *testing.T) { tk := testkit.NewTestKit(t, store) tk.MustExec("use test") + tk.MustExec("set @@foreign_key_checks=0") tk.MustExec("create table test.t_parser_issue_284(c1 int not null primary key)") _, err := tk.Exec("create table test.t_parser_issue_284_2(id int not null primary key, c1 int not null, constraint foreign key (c1) references t_parser_issue_284(c1))") require.NoError(t, err) diff --git a/ddl/serial_test.go b/ddl/serial_test.go index 0d9636ffa4180..e3456124871e8 100644 --- a/ddl/serial_test.go +++ b/ddl/serial_test.go @@ -112,6 +112,7 @@ func TestCreateTableWithLike(t *testing.T) { tk.MustExec("use ctwl_db") tk.MustExec("create table tt(id int primary key)") tk.MustExec("create table t (c1 int not null auto_increment, c2 int, constraint cc foreign key (c2) references tt(id), primary key(c1)) auto_increment = 10") + tk.MustExec("set @@foreign_key_checks=0") tk.MustExec("insert into t set c2=1") tk.MustExec("create table t1 like ctwl_db.t") tk.MustExec("insert into t1 set c2=11") diff --git a/executor/explain_test.go b/executor/explain_test.go index bbabaef759e3b..28dbbbe12fcf6 100644 --- a/executor/explain_test.go +++ b/executor/explain_test.go @@ -247,6 +247,7 @@ func TestExplainAnalyzeExecutionInfo(t *testing.T) { checkExecutionInfo(t, tk, "explain analyze select * from t use index(k)") checkExecutionInfo(t, tk, "explain analyze with recursive cte(a) as (select 1 union select a + 1 from cte where a < 1000) select * from cte;") + tk.MustExec("set @@foreign_key_checks=0") tk.MustExec("CREATE TABLE IF NOT EXISTS nation ( N_NATIONKEY BIGINT NOT NULL,N_NAME CHAR(25) NOT NULL,N_REGIONKEY BIGINT NOT NULL,N_COMMENT VARCHAR(152),PRIMARY KEY (N_NATIONKEY));") tk.MustExec("CREATE TABLE IF NOT EXISTS part ( P_PARTKEY BIGINT NOT NULL,P_NAME VARCHAR(55) NOT NULL,P_MFGR CHAR(25) NOT NULL,P_BRAND CHAR(10) NOT NULL,P_TYPE VARCHAR(25) NOT NULL,P_SIZE BIGINT NOT NULL,P_CONTAINER CHAR(10) NOT NULL,P_RETAILPRICE DECIMAL(15,2) NOT NULL,P_COMMENT VARCHAR(23) NOT NULL,PRIMARY KEY (P_PARTKEY));") tk.MustExec("CREATE TABLE IF NOT EXISTS supplier ( S_SUPPKEY BIGINT NOT NULL,S_NAME CHAR(25) NOT NULL,S_ADDRESS VARCHAR(40) NOT NULL,S_NATIONKEY BIGINT NOT NULL,S_PHONE CHAR(15) NOT NULL,S_ACCTBAL DECIMAL(15,2) NOT NULL,S_COMMENT VARCHAR(101) NOT NULL,PRIMARY KEY (S_SUPPKEY),CONSTRAINT FOREIGN KEY SUPPLIER_FK1 (S_NATIONKEY) references nation(N_NATIONKEY));") diff --git a/executor/issuetest/executor_issue_test.go b/executor/issuetest/executor_issue_test.go index 9336cae75eaeb..9e28feca1530f 100644 --- a/executor/issuetest/executor_issue_test.go +++ b/executor/issuetest/executor_issue_test.go @@ -532,6 +532,7 @@ func TestFix31537(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) tk.MustExec("use test") + tk.MustExec("set @@foreign_key_checks=0") tk.MustExec(`CREATE TABLE trade ( t_id bigint(16) NOT NULL AUTO_INCREMENT, t_dts datetime NOT NULL, diff --git a/executor/set_test.go b/executor/set_test.go index 31540af3e1bf6..75b6c210146b7 100644 --- a/executor/set_test.go +++ b/executor/set_test.go @@ -799,15 +799,15 @@ func TestSetVar(t *testing.T) { // test variable 'foreign_key_checks' // global scope - tk.MustQuery("select @@global.foreign_key_checks").Check(testkit.Rows("0")) // default value - tk.MustExec("set global foreign_key_checks = 1") - tk.MustQuery("select @@global.foreign_key_checks").Check(testkit.Rows("1")) + tk.MustQuery("select @@global.foreign_key_checks").Check(testkit.Rows("1")) // default value + tk.MustExec("set global foreign_key_checks = 0") + tk.MustQuery("select @@global.foreign_key_checks").Check(testkit.Rows("0")) // session scope - tk.MustQuery("select @@session.foreign_key_checks").Check(testkit.Rows("0")) // default value - tk.MustExec("set session foreign_key_checks = 1") - tk.MustQuery("select @@session.foreign_key_checks").Check(testkit.Rows("1")) + tk.MustQuery("select @@session.foreign_key_checks").Check(testkit.Rows("1")) // default value + tk.MustExec("set session foreign_key_checks = 0") + tk.MustQuery("select @@session.foreign_key_checks").Check(testkit.Rows("0")) - // test variable 'foreign_key_checks' + // test variable 'tidb_enable_foreign_key' // global scope tk.MustQuery("select @@global.tidb_enable_foreign_key").Check(testkit.Rows("1")) // default value tk.MustExec("set global tidb_enable_foreign_key = 0") diff --git a/executor/showtest/show_test.go b/executor/showtest/show_test.go index 75afeb0c5f265..de11863b93a33 100644 --- a/executor/showtest/show_test.go +++ b/executor/showtest/show_test.go @@ -318,9 +318,10 @@ func TestShowCreateTable(t *testing.T) { ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", )) - // TiDB defaults (and only supports) foreign_key_checks=0 + // set @@foreign_key_checks=0, // This means that the child table can be created before the parent table. // This behavior is required for mysqldump restores. + tk.MustExec("set @@foreign_key_checks=0") tk.MustExec(`DROP TABLE IF EXISTS parent, child`) tk.MustExec(`CREATE TABLE child (id INT NOT NULL PRIMARY KEY auto_increment, parent_id INT NOT NULL, INDEX par_ind (parent_id), CONSTRAINT child_ibfk_1 FOREIGN KEY (parent_id) REFERENCES parent(id))`) tk.MustExec(`CREATE TABLE parent ( id INT NOT NULL PRIMARY KEY auto_increment )`) diff --git a/planner/core/point_get_plan.go b/planner/core/point_get_plan.go index de69438257f4e..4ff2a319c2d12 100644 --- a/planner/core/point_get_plan.go +++ b/planner/core/point_get_plan.go @@ -1687,10 +1687,12 @@ func buildPointDeletePlan(ctx sessionctx.Context, pointPlan PhysicalPlan, dbName var err error is := sessiontxn.GetTxnManager(ctx).GetTxnInfoSchema() t, _ := is.TableByID(tbl.ID) - tblID2Table := map[int64]table.Table{tbl.ID: t} - err = delPlan.buildOnDeleteFKTriggers(ctx, is, tblID2Table) - if err != nil { - return nil + if t != nil { + tblID2Table := map[int64]table.Table{tbl.ID: t} + err = delPlan.buildOnDeleteFKTriggers(ctx, is, tblID2Table) + if err != nil { + return nil + } } return delPlan } diff --git a/session/session_test/session_test.go b/session/session_test/session_test.go index 60b265b3caa6a..06f79d352ee76 100644 --- a/session/session_test/session_test.go +++ b/session/session_test/session_test.go @@ -2961,6 +2961,7 @@ func TestIgnoreForeignKey(t *testing.T) { tk := testkit.NewTestKit(t, store) tk.MustExec("use test") + tk.MustExec("set @@foreign_key_checks=0") sqlText := `CREATE TABLE address ( id bigint(20) NOT NULL AUTO_INCREMENT, user_id bigint(20) NOT NULL, diff --git a/sessionctx/variable/tidb_vars.go b/sessionctx/variable/tidb_vars.go index 9c564aca2dc19..39134d62894db 100644 --- a/sessionctx/variable/tidb_vars.go +++ b/sessionctx/variable/tidb_vars.go @@ -1110,7 +1110,7 @@ const ( DefTiDBAutoBuildStatsConcurrency = 1 DefTiDBSysProcScanConcurrency = 1 DefTiDBRcWriteCheckTs = false - DefTiDBForeignKeyChecks = false + DefTiDBForeignKeyChecks = true DefTiDBAnalyzePartitionConcurrency = 1 DefTiDBOptRangeMaxSize = 64 * int64(size.MB) // 64 MB DefTiDBCostModelVer = 2