Skip to content

Commit

Permalink
executor: show create table should distinguish between old and new fo…
Browse files Browse the repository at this point in the history
…reign key (#38719)

close #38717
  • Loading branch information
crazycs520 authored Oct 28, 2022
1 parent e415cfa commit ba2e2c9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
27 changes: 27 additions & 0 deletions executor/fktest/foreign_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1584,3 +1584,30 @@ func TestForeignKeyOnDeleteSetNull2(t *testing.T) {
tk.MustQuery("select count(*) from t1").Check(testkit.Rows("0"))
tk.MustQuery("select count(*) from t2 where id is null").Check(testkit.Rows("32768"))
}

func TestShowCreateTableWithForeignKey(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")

tk.MustExec("create table t1 (id int key, leader int, leader2 int, index(leader), index(leader2), constraint fk foreign key (leader) references t1(id) ON DELETE CASCADE ON UPDATE SET NULL);")
tk.MustQuery("show create table t1").Check(testkit.Rows("t1 CREATE TABLE `t1` (\n" +
" `id` int(11) NOT NULL,\n" +
" `leader` int(11) DEFAULT NULL,\n" +
" `leader2` int(11) DEFAULT NULL,\n" +
" PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,\n" +
" KEY `leader` (`leader`),\n KEY `leader2` (`leader2`),\n" +
" CONSTRAINT `fk` FOREIGN KEY (`leader`) REFERENCES `test`.`t1` (`id`) ON DELETE CASCADE ON UPDATE SET NULL /*T![FOREIGN KEY] INVALID */\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
tk.MustExec("set @@global.tidb_enable_foreign_key=1")
tk.MustExec("alter table t1 add constraint fk2 foreign key (leader2) references t1 (id)")
tk.MustQuery("show create table t1").Check(testkit.Rows("t1 CREATE TABLE `t1` (\n" +
" `id` int(11) NOT NULL,\n" +
" `leader` int(11) DEFAULT NULL,\n" +
" `leader2` int(11) DEFAULT NULL,\n" +
" PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,\n" +
" KEY `leader` (`leader`),\n KEY `leader2` (`leader2`),\n" +
" CONSTRAINT `fk` FOREIGN KEY (`leader`) REFERENCES `test`.`t1` (`id`) ON DELETE CASCADE ON UPDATE SET NULL /*T![FOREIGN KEY] INVALID */,\n" +
" CONSTRAINT `fk2` FOREIGN KEY (`leader2`) REFERENCES `test`.`t1` (`id`)\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
}
3 changes: 3 additions & 0 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,9 @@ func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.T
if model.ReferOptionType(fk.OnUpdate) != 0 {
buf.WriteString(fmt.Sprintf(" ON UPDATE %s", model.ReferOptionType(fk.OnUpdate).String()))
}
if fk.Version < model.FKVersion1 {
buf.WriteString(" /*T![FOREIGN KEY] INVALID */")
}
}

buf.WriteString("\n")
Expand Down
6 changes: 3 additions & 3 deletions executor/showtest/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func TestShowCreateTable(t *testing.T) {
" `parent_id` int(11) NOT NULL,\n"+
" PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,\n"+
" KEY `par_ind` (`parent_id`),\n"+
" CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `test`.`parent` (`id`)\n"+
" CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `test`.`parent` (`id`) /*T![FOREIGN KEY] INVALID */\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin",
))

Expand All @@ -344,7 +344,7 @@ func TestShowCreateTable(t *testing.T) {
" `parent_id` int(11) NOT NULL,\n"+
" PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,\n"+
" KEY `par_ind` (`parent_id`),\n"+
" CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `test`.`parent` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE\n"+
" CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `test`.`parent` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE /*T![FOREIGN KEY] INVALID */\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin",
))

Expand All @@ -357,7 +357,7 @@ func TestShowCreateTable(t *testing.T) {
" `id` int(11) NOT NULL,\n" +
" `b` int(11) DEFAULT NULL,\n" +
" PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,\n" +
" CONSTRAINT `fk` FOREIGN KEY (`b`) REFERENCES `test1`.`t1` (`id`)\n" +
" CONSTRAINT `fk` FOREIGN KEY (`b`) REFERENCES `test1`.`t1` (`id`) /*T![FOREIGN KEY] INVALID */\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))

// Test issue #20327
Expand Down

0 comments on commit ba2e2c9

Please sign in to comment.