diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index a19d1f178749d..6a8b984a4d953 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -4546,7 +4546,7 @@ func checkExchangePartition(pt *model.TableInfo, nt *model.TableInfo) error { return errors.Trace(dbterror.ErrPartitionExchangePartTable.GenWithStackByArgs(nt.Name)) } - if nt.ForeignKeys != nil { + if len(nt.ForeignKeys) > 0 { return errors.Trace(dbterror.ErrPartitionExchangeForeignKey.GenWithStackByArgs(nt.Name)) } diff --git a/ddl/integration_test.go b/ddl/integration_test.go index 46f26fa5e8237..4a00bab84a16b 100644 --- a/ddl/integration_test.go +++ b/ddl/integration_test.go @@ -142,3 +142,18 @@ func TestDDLOnCachedTable(t *testing.T) { tk.MustExec("alter table t nocache;") tk.MustExec("drop table if exists t;") } + +func TestExchangePartitionAfterDropForeignKey(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + + tk.MustExec("use test;") + + tk.MustExec("create table parent (id int unique);") + tk.MustExec("create table child (id int, parent_id int, foreign key (parent_id) references parent(id));") + tk.MustExec("create table child_with_partition(id int, parent_id int) partition by range(id) (partition p1 values less than (100));") + tk.MustGetErrMsg("alter table child_with_partition exchange partition p1 with table child;", "[ddl:1740]Table to exchange with partition has foreign key references: 'child'") + tk.MustExec("alter table child drop foreign key fk_1;") + tk.MustExec("alter table child drop key fk_1;") + tk.MustExec("alter table child_with_partition exchange partition p1 with table child;") +}