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

executor: align behavior of foreign keys in "SHOW CREATE TABLE" with MySQL. | tidb-test=pr/2312 #51294

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 3 additions & 3 deletions pkg/ddl/tests/fk/foreign_key_test.go
Expand Up @@ -358,7 +358,7 @@ func TestRenameTableWithForeignKeyMetaInfo(t *testing.T) {
" `b` int(11) DEFAULT NULL,\n" +
" PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,\n" +
" KEY `fk` (`a`),\n" +
" CONSTRAINT `fk` FOREIGN KEY (`a`) REFERENCES `test2`.`t2` (`id`)\n" +
" CONSTRAINT `fk` FOREIGN KEY (`a`) REFERENCES `t2` (`id`)\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
tblInfo := getTableInfo(t, dom, "test2", "t2")
tbReferredFKs := getTableInfoReferredForeignKeys(t, dom, "test2", "t2")
Expand Down Expand Up @@ -1053,8 +1053,8 @@ func TestRenameColumnWithForeignKeyMetaInfo(t *testing.T) {
" `aa` int(11) DEFAULT NULL,\n" +
" `bb` int(11) DEFAULT NULL,\n" +
" KEY `fk_1` (`aa`),\n KEY `fk_2` (`bb`),\n" +
" CONSTRAINT `fk_1` FOREIGN KEY (`aa`) REFERENCES `test`.`t1` (`bb`),\n" +
" CONSTRAINT `fk_2` FOREIGN KEY (`bb`) REFERENCES `test`.`t1` (`bb`)\n" +
" CONSTRAINT `fk_1` FOREIGN KEY (`aa`) REFERENCES `t1` (`bb`),\n" +
" CONSTRAINT `fk_2` FOREIGN KEY (`bb`) REFERENCES `t1` (`bb`)\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/executor/show.go
Expand Up @@ -1172,7 +1172,7 @@ func constructResultOfShowCreateTable(ctx sessionctx.Context, dbName *model.CISt
colNames = append(colNames, stringutil.Escape(col.O, sqlMode))
}
fmt.Fprintf(buf, "(%s)", strings.Join(colNames, ","))
if fk.RefSchema.L != "" {
if fk.RefSchema.L != "" && dbName != nil && fk.RefSchema.L != dbName.L {
fmt.Fprintf(buf, " REFERENCES %s.%s ", stringutil.Escape(fk.RefSchema.O, sqlMode), stringutil.Escape(fk.RefTable.O, sqlMode))
} else {
fmt.Fprintf(buf, " REFERENCES %s ", stringutil.Escape(fk.RefTable.O, sqlMode))
Expand Down
6 changes: 3 additions & 3 deletions tests/integrationtest/r/ddl/column_type_change.result
Expand Up @@ -2066,7 +2066,7 @@ orders CREATE TABLE `orders` (
`doc` json DEFAULT NULL,
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,
KEY `fk_user_id` (`user_id`),
CONSTRAINT `fk_user_id` FOREIGN KEY (`user_id`) REFERENCES `ddl__column_type_change`.`users` (`id`)
CONSTRAINT `fk_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
drop table if exists users, orders;
CREATE TABLE users (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, doc JSON);
Expand All @@ -2080,7 +2080,7 @@ orders CREATE TABLE `orders` (
`doc` json DEFAULT NULL,
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,
KEY `fk_user_id` (`user_id2`),
CONSTRAINT `fk_user_id` FOREIGN KEY (`user_id2`) REFERENCES `ddl__column_type_change`.`users` (`id`)
CONSTRAINT `fk_user_id` FOREIGN KEY (`user_id2`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
drop table if exists users, orders;
CREATE TABLE users (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, doc JSON);
Expand All @@ -2094,7 +2094,7 @@ orders CREATE TABLE `orders` (
`doc` json DEFAULT NULL,
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,
KEY `fk_user_id` (`user_id`),
CONSTRAINT `fk_user_id` FOREIGN KEY (`user_id`) REFERENCES `ddl__column_type_change`.`users` (`id`)
CONSTRAINT `fk_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
drop table if exists users, orders;
CREATE TABLE users (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, doc JSON);
Expand Down
4 changes: 2 additions & 2 deletions tests/integrationtest/r/ddl/foreign_key.result
Expand Up @@ -132,8 +132,8 @@ t2 CREATE TABLE `t2` (
`b` int(11) DEFAULT NULL,
KEY `fk_1` (`a`),
KEY `fk_2` (`b`),
CONSTRAINT `fk_1` FOREIGN KEY (`a`) REFERENCES `ddl__foreign_key`.`t1` (`id`),
CONSTRAINT `fk_2` FOREIGN KEY (`b`) REFERENCES `ddl__foreign_key`.`t1` (`id`)
CONSTRAINT `fk_1` FOREIGN KEY (`a`) REFERENCES `t1` (`id`),
CONSTRAINT `fk_2` FOREIGN KEY (`b`) REFERENCES `t1` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
drop table t2;
create table t2 (a int, b int, index idx0(a,b), index idx1(a), index idx2(b));
Expand Down
30 changes: 27 additions & 3 deletions tests/integrationtest/r/executor/foreign_key.result
Expand Up @@ -115,7 +115,7 @@ t1 CREATE TABLE `t1` (
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,
KEY `leader` (`leader`),
KEY `leader2` (`leader2`),
CONSTRAINT `fk` FOREIGN KEY (`leader`) REFERENCES `executor__foreign_key`.`t1` (`id`) ON DELETE CASCADE ON UPDATE SET NULL /* FOREIGN KEY INVALID */
CONSTRAINT `fk` FOREIGN KEY (`leader`) REFERENCES `t1` (`id`) ON DELETE CASCADE ON UPDATE SET NULL /* FOREIGN KEY INVALID */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
set @@global.tidb_enable_foreign_key=1;
alter table t1 add constraint fk2 foreign key (leader2) references t1 (id);
Expand All @@ -128,8 +128,8 @@ t1 CREATE TABLE `t1` (
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,
KEY `leader` (`leader`),
KEY `leader2` (`leader2`),
CONSTRAINT `fk` FOREIGN KEY (`leader`) REFERENCES `executor__foreign_key`.`t1` (`id`) ON DELETE CASCADE ON UPDATE SET NULL /* FOREIGN KEY INVALID */,
CONSTRAINT `fk2` FOREIGN KEY (`leader2`) REFERENCES `executor__foreign_key`.`t1` (`id`)
CONSTRAINT `fk` FOREIGN KEY (`leader`) REFERENCES `t1` (`id`) ON DELETE CASCADE ON UPDATE SET NULL /* FOREIGN KEY INVALID */,
CONSTRAINT `fk2` FOREIGN KEY (`leader2`) REFERENCES `t1` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
drop table t1;
create table t1 (id int key, leader int, leader2 int, index(leader), index(leader2), constraint fk foreign key (leader) references t1(id) /* FOREIGN KEY INVALID */);
Expand Down Expand Up @@ -454,3 +454,27 @@ id
3
set @@global.tidb_enable_foreign_key=default;
set @@foreign_key_checks=default;
set @@global.tidb_enable_foreign_key=1;
set @@foreign_key_checks=1;
create database executor__foreign_key_other_schema;
use executor__foreign_key_other_schema;
drop table if exists users;
CREATE TABLE users (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, doc JSON);
use executor__foreign_key;
drop table if exists orders;
CREATE TABLE orders (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, user_id INT NOT NULL, doc JSON, FOREIGN KEY fk_user_id (user_id) REFERENCES executor__foreign_key_other_schema.users(id));
alter table orders modify user_id int null;
show create table orders;
Table Create Table
orders CREATE TABLE `orders` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`doc` json DEFAULT NULL,
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,
KEY `fk_user_id` (`user_id`),
CONSTRAINT `fk_user_id` FOREIGN KEY (`user_id`) REFERENCES `executor__foreign_key_other_schema`.`users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
drop table if exists orders;
drop database executor__foreign_key_other_schema;
set @@global.tidb_enable_foreign_key=default;
set @@foreign_key_checks=default;
4 changes: 2 additions & 2 deletions tests/integrationtest/r/executor/show.result
Expand Up @@ -370,7 +370,7 @@ child CREATE TABLE `child` (
`parent_id` int(11) NOT NULL,
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,
KEY `par_ind` (`parent_id`),
CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `executor__show`.`parent` (`id`)
CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
DROP TABLE child;
CREATE TABLE child (id INT NOT NULL PRIMARY KEY auto_increment, parent_id INT NOT NULL, INDEX par_ind (parent_id), CONSTRAINT child_ibfk_1 FOREIGN KEY (parent_id) REFERENCES parent(id) ON DELETE RESTRICT ON UPDATE CASCADE);
Expand All @@ -381,7 +381,7 @@ child CREATE TABLE `child` (
`parent_id` int(11) NOT NULL,
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,
KEY `par_ind` (`parent_id`),
CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `executor__show`.`parent` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
create database test1;
create database test2;
Expand Down
16 changes: 16 additions & 0 deletions tests/integrationtest/t/executor/foreign_key.test
Expand Up @@ -377,3 +377,19 @@ disconnect conn1;
set @@global.tidb_enable_foreign_key=default;
set @@foreign_key_checks=default;

# TestForeignKeyOtherSchema
set @@global.tidb_enable_foreign_key=1;
set @@foreign_key_checks=1;
create database executor__foreign_key_other_schema;
use executor__foreign_key_other_schema;
drop table if exists users;
CREATE TABLE users (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, doc JSON);
use executor__foreign_key;
drop table if exists orders;
CREATE TABLE orders (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, user_id INT NOT NULL, doc JSON, FOREIGN KEY fk_user_id (user_id) REFERENCES executor__foreign_key_other_schema.users(id));
alter table orders modify user_id int null;
show create table orders;
drop table if exists orders;
drop database executor__foreign_key_other_schema;
set @@global.tidb_enable_foreign_key=default;
set @@foreign_key_checks=default;