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

ddl: fix issue of partition table with foreign key and add more test case #40122

Merged
merged 4 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
92 changes: 92 additions & 0 deletions ddl/fktest/foreign_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,28 @@ func TestCreateTableWithForeignKeyError(t *testing.T) {
create: "create table t2 (id int key, constraint fk foreign key (id) references t1(name5678901234567890123456789012345678901234567890123456789012345));",
err: "[ddl:1059]Identifier name 'name5678901234567890123456789012345678901234567890123456789012345' is too long",
},
// Test foreign key with temporary table
{
refer: "create temporary table t1 (id int key);",
create: "create table t2 (id int key, constraint fk foreign key (id) references t1(id));",
err: "[schema:1824]Failed to open the referenced table 't1'",
},
{
refer: "create table t1 (id int key);",
create: "create temporary table t2 (id int key, constraint fk foreign key (id) references t1(id));",
err: "[schema:1215]Cannot add foreign key constraint",
},
// Test foreign key with partition table
{
refer: "create table t1 (id int key) partition by hash(id) partitions 3;",
create: "create table t2 (id int key, constraint fk foreign key (id) references t1(id));",
err: "[schema:1506]Foreign key clause is not yet supported in conjunction with partitioning",
},
{
refer: "create table t1 (id int key);",
create: "create table t2 (id int key, constraint fk foreign key (id) references t1(id)) partition by hash(id) partitions 3;",
err: "[schema:1506]Foreign key clause is not yet supported in conjunction with partitioning",
},
}
for _, ca := range cases {
tk.MustExec("drop table if exists t2")
Expand Down Expand Up @@ -1415,6 +1437,40 @@ func TestAlterTableAddForeignKeyError(t *testing.T) {
alter: "alter table t2 add constraint name5678901234567890123456789012345678901234567890123456789012345 foreign key (b) references t1(id)",
err: "[ddl:1059]Identifier name 'name5678901234567890123456789012345678901234567890123456789012345' is too long",
},
// Test foreign key with temporary table.
{
prepares: []string{
"create temporary table t1 (id int key);",
"create table t2 (a int, b int unique);",
},
alter: "alter table t2 add constraint fk foreign key (b) references t1(id)",
err: "[schema:1824]Failed to open the referenced table 't1'",
},
{
prepares: []string{
"create table t1 (id int key);",
"create temporary table t2 (a int, b int unique);",
},
alter: "alter table t2 add constraint fk foreign key (b) references t1(id)",
err: "[ddl:8200]TiDB doesn't support ALTER TABLE for local temporary table",
},
// Test foreign key with partition table
{
prepares: []string{
"create table t1 (id int key) partition by hash(id) partitions 3;",
"create table t2 (id int key);",
},
alter: "alter table t2 add constraint fk foreign key (id) references t1(id)",
err: "[schema:1506]Foreign key clause is not yet supported in conjunction with partitioning",
},
{
prepares: []string{
"create table t1 (id int key);",
"create table t2 (id int key) partition by hash(id) partitions 3;;",
},
alter: "alter table t2 add constraint fk foreign key (id) references t1(id)",
err: "[schema:1506]Foreign key clause is not yet supported in conjunction with partitioning",
},
}
for i, ca := range cases {
tk.MustExec("drop table if exists t2")
Expand Down Expand Up @@ -1608,3 +1664,39 @@ func TestAddForeignKeyInBigTable(t *testing.T) {
tk.MustExec("alter table employee add foreign key fk_1(pid) references employee(id)")
require.Less(t, time.Since(start), time.Minute)
}

func TestForeignKeyWithCacheTable(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("set @@foreign_key_checks=1;")
tk.MustExec("use test")
// Test foreign key refer cache table.
tk.MustExec("create table t1 (id int key);")
tk.MustExec("insert into t1 values (1),(2),(3),(4)")
tk.MustExec("alter table t1 cache;")
tk.MustExec("create table t2 (b int);")
tk.MustExec("alter table t2 add constraint fk foreign key (b) references t1(id) on delete cascade on update cascade")
tk.MustExec("insert into t2 values (1),(2),(3),(4)")
tk.MustGetDBError("insert into t2 values (5)", plannercore.ErrNoReferencedRow2)
tk.MustExec("update t1 set id = id+10 where id=1")
tk.MustExec("delete from t1 where id<10")
tk.MustQuery("select * from t1").Check(testkit.Rows("11"))
tk.MustQuery("select * from t2").Check(testkit.Rows("11"))
tk.MustExec("alter table t1 nocache;")
tk.MustExec("drop table t1,t2;")

// Test add foreign key on cache table.
tk.MustExec("create table t1 (id int key);")
tk.MustExec("create table t2 (b int);")
tk.MustExec("alter table t2 add constraint fk foreign key (b) references t1(id) on delete cascade on update cascade")
tk.MustExec("alter table t2 cache;")
tk.MustExec("insert into t1 values (1),(2),(3),(4)")
tk.MustExec("insert into t2 values (1),(2),(3),(4)")
tk.MustGetDBError("insert into t2 values (5)", plannercore.ErrNoReferencedRow2)
tk.MustExec("update t1 set id = id+10 where id=1")
tk.MustExec("delete from t1 where id<10")
tk.MustQuery("select * from t1").Check(testkit.Rows("11"))
tk.MustQuery("select * from t2").Check(testkit.Rows("11"))
tk.MustExec("alter table t2 nocache;")
tk.MustExec("drop table t1,t2;")
}
3 changes: 3 additions & 0 deletions ddl/foreign_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ func checkTableForeignKey(referTblInfo, tblInfo *model.TableInfo, fkInfo *model.
if referTblInfo.TTLInfo != nil {
return dbterror.ErrUnsupportedTTLReferencedByFK
}
if referTblInfo.GetPartitionInfo() != nil || tblInfo.GetPartitionInfo() != nil {
return infoschema.ErrForeignKeyOnPartitioned
}

// check refer columns in parent table.
for i := range fkInfo.RefCols {
Expand Down
5 changes: 5 additions & 0 deletions errors.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2411,6 +2411,11 @@ error = '''
Changing schema from '%-.192s' to '%-.192s' is not allowed.
'''

["schema:1506"]
error = '''
Foreign key clause is not yet supported in conjunction with partitioning
'''

["schema:1822"]
error = '''
Failed to add the foreign key constraint. Missing index for constraint '%s' in the referenced table '%s'
Expand Down
2 changes: 2 additions & 0 deletions infoschema/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ var (
ErrKeyNotExists = dbterror.ClassSchema.NewStd(mysql.ErrKeyDoesNotExist)
// ErrCannotAddForeign returns for foreign key exists.
ErrCannotAddForeign = dbterror.ClassSchema.NewStd(mysql.ErrCannotAddForeign)
// ErrForeignKeyOnPartitioned returns for foreign key on partition table.
ErrForeignKeyOnPartitioned = dbterror.ClassSchema.NewStd(mysql.ErrForeignKeyOnPartitioned)
// ErrForeignKeyNotMatch returns for foreign key not match.
ErrForeignKeyNotMatch = dbterror.ClassSchema.NewStd(mysql.ErrWrongFkDef)
// ErrIndexExists returns for index already exists.
Expand Down