From 661a6b60dd231d240b2df83a3aebb5e6d9cd1474 Mon Sep 17 00:00:00 2001 From: wxbty <38374721+wxbty@users.noreply.github.com> Date: Tue, 15 Nov 2022 16:25:54 +0800 Subject: [PATCH] ddl: modify default unique index name (#39145) close pingcap/tidb#39080 --- ddl/column_test.go | 26 ++++++++++++++++++++++++++ ddl/ddl_api.go | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/ddl/column_test.go b/ddl/column_test.go index cae9a27318dec..d11d33e213d6a 100644 --- a/ddl/column_test.go +++ b/ddl/column_test.go @@ -959,3 +959,29 @@ func TestGetDefaultValueOfColumn(t *testing.T) { tk.MustQuery("select * from t1").Check(testkit.RowsWithSep("|", ""+ "1962-03-03 1962-03-03 00:00:00 12:23:23 2020-10-13 2020-03-27")) } + +func TestIssue39080(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("CREATE TABLE t1(id INTEGER PRIMARY KEY, authorId INTEGER AUTO_INCREMENT UNIQUE)") + + tk.MustQuery("show create table t1").Check(testkit.RowsWithSep("|", ""+ + "t1 CREATE TABLE `t1` (\n"+ + " `id` int(11) NOT NULL,\n"+ + " `authorId` int(11) NOT NULL AUTO_INCREMENT,\n"+ + " PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,\n"+ + " UNIQUE KEY `authorId` (`authorId`)\n"+ + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) + + //Do not affect the specified name + tk.MustExec("CREATE TABLE `t2`( `id` INTEGER PRIMARY KEY, `authorId` int(11) AUTO_INCREMENT, UNIQUE KEY `authorIdx` (`authorId`))") + + tk.MustQuery("show create table t2").Check(testkit.RowsWithSep("|", ""+ + "t2 CREATE TABLE `t2` (\n"+ + " `id` int(11) NOT NULL,\n"+ + " `authorId` int(11) NOT NULL AUTO_INCREMENT,\n"+ + " PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,\n"+ + " UNIQUE KEY `authorIdx` (`authorId`)\n"+ + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) +} diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index bbf3d4fc59856..6111dd3fb4fdb 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -1693,7 +1693,7 @@ func setEmptyConstraintName(namesMap map[string]bool, constr *ast.Constraint) { } } if colName == "" { - colName = constr.Keys[0].Column.Name.L + colName = constr.Keys[0].Column.Name.O } constrName := colName i := 2