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 the wrong logic to assert whether table has foreign key (#51808) #51821

Merged
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
2 changes: 1 addition & 1 deletion ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
15 changes: 15 additions & 0 deletions ddl/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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;")
}