@@ -402,3 +402,41 @@ func TestAddForeignKey3(t *testing.T) {
402
402
tk .MustQuery ("select * from t1 order by id" ).Check (testkit .Rows ("1 1" , "2 2" , "3 3" ))
403
403
tk .MustQuery ("select * from t2 order by id" ).Check (testkit .Rows ("1 1" , "2 2" , "3 3" ))
404
404
}
405
+
406
+ func TestForeignKeyInWriteOnlyMode (t * testing.T ) {
407
+ store := testkit .CreateMockStore (t )
408
+ tk := testkit .NewTestKit (t , store )
409
+ tk .MustExec ("use test" )
410
+
411
+ tkDDL := testkit .NewTestKit (t , store )
412
+ tkDDL .MustExec ("use test" )
413
+ tkDDL .MustExec ("create table parent (id int key)" )
414
+ tkDDL .MustExec ("insert into parent values(1)" )
415
+
416
+ var notExistErrs []error
417
+ testfailpoint .EnableCall (t , "github.com/pingcap/tidb/pkg/ddl/onJobRunBefore" , func (job * model.Job ) {
418
+ if job .Type == model .ActionCreateTable && job .TableName == "child" {
419
+ if job .SchemaState == model .StateWriteOnly {
420
+ // tk with the latest schema will insert data into child
421
+ _ , err := tk .Exec ("insert into child values (1, 1)" )
422
+ notExistErrs = append (notExistErrs , err )
423
+ _ , err = tk .Exec ("update child set id = 2 where id = 1" )
424
+ notExistErrs = append (notExistErrs , err )
425
+ _ , err = tk .Exec ("delete from child where id = 1" )
426
+ notExistErrs = append (notExistErrs , err )
427
+ _ , err = tk .Exec ("delete child from child inner join parent where child.pid = parent.id" )
428
+ notExistErrs = append (notExistErrs , err )
429
+ _ , err = tk .Exec ("delete parent from child inner join parent where child.pid = parent.id" )
430
+ notExistErrs = append (notExistErrs , err )
431
+ }
432
+ }
433
+ })
434
+ tkDDL .MustExec ("create table child (id int, pid int, index idx_pid(pid), foreign key (pid) references parent(id) on delete cascade);" )
435
+
436
+ testfailpoint .Disable (t , "github.com/pingcap/tidb/pkg/ddl/onJobRunBefore" )
437
+
438
+ for _ , err := range notExistErrs {
439
+ require .Error (t , err )
440
+ require .Contains (t , err .Error (), "Table 'test.child' doesn't exist" )
441
+ }
442
+ }
0 commit comments