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鈥檒l 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

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/ddl/ddl_api.go
Expand Up @@ -5225,7 +5225,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
9 changes: 9 additions & 0 deletions tests/integrationtest/r/ddl/partition.result
Expand Up @@ -457,3 +457,12 @@ drop table t;
set character_set_connection=DEFAULT;
create table a (col1 int, col2 int, unique key (col1, col2)) partition by range columns (col1, col2) (partition p0 values less than (NULL, 1 ));
Error 1566 (HY000): Not allowed to use NULL value in VALUES LESS THAN
drop table if exists parent, child, child_with_partition;
create table parent (id int unique);
create table child (id int, parent_id int, foreign key (parent_id) references parent(id));
create table child_with_partition(id int, parent_id int) partition by range(id) (partition p1 values less than (100));
alter table child_with_partition exchange partition p1 with table child;
Error 1740 (HY000): Table to exchange with partition has foreign key references: 'child'
alter table child drop foreign key fk_1;
alter table child drop key fk_1;
alter table child_with_partition exchange partition p1 with table child;
10 changes: 10 additions & 0 deletions tests/integrationtest/t/ddl/partition.test
Expand Up @@ -259,3 +259,13 @@ set character_set_connection=DEFAULT;
-- error 1566
create table a (col1 int, col2 int, unique key (col1, col2)) partition by range columns (col1, col2) (partition p0 values less than (NULL, 1 ));

# TestExchangePartitionAfterDropForeignKey
drop table if exists parent, child, child_with_partition;
create table parent (id int unique);
create table child (id int, parent_id int, foreign key (parent_id) references parent(id));
create table child_with_partition(id int, parent_id int) partition by range(id) (partition p1 values less than (100));
-- error 1740
alter table child_with_partition exchange partition p1 with table child;
alter table child drop foreign key fk_1;
alter table child drop key fk_1;
alter table child_with_partition exchange partition p1 with table child;