diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index 964fda974ca42..3e28686ad2302 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -3621,6 +3621,22 @@ func (s *testIntegrationSuite3) TestIssue29282(c *C) { } } +// See https://github.com/pingcap/tidb/issues/29327 +func (s *testIntegrationSuite3) TestEnumDefaultValue(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t1;") + tk.MustExec("CREATE TABLE `t1` ( `a` enum('','a','b') NOT NULL DEFAULT 'b' ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;") + tk.MustQuery("show create table t1").Check(testkit.Rows("t1 CREATE TABLE `t1` (\n" + + " `a` enum('','a','b') COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT 'b'\n" + + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci")) + tk.MustExec("drop table if exists t1;") + tk.MustExec("CREATE TABLE `t1` ( `a` enum('','a','b') NOT NULL DEFAULT 'b ' ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;") + tk.MustQuery("show create table t1").Check(testkit.Rows("t1 CREATE TABLE `t1` (\n" + + " `a` enum('','a','b') COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT 'b'\n" + + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci")) +} + func (s *testIntegrationSuite3) TestIssue29326(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 9897793e4dcf4..7ff671fa4f6ea 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -998,11 +998,13 @@ func getEnumDefaultValue(v types.Datum, col *table.Column) (string, error) { v.SetMysqlEnum(enumVal, col.Collate) return v.ToString() } - str, err := v.ToString() if err != nil { return "", errors.Trace(err) } + // Ref: https://dev.mysql.com/doc/refman/8.0/en/enum.html + // Trailing spaces are automatically deleted from ENUM member values in the table definition when a table is created. + str = strings.TrimRight(str, " ") enumVal, err := types.ParseEnumName(col.Elems, str, col.Collate) if err != nil { return "", ErrInvalidDefaultValue.GenWithStackByArgs(col.Name.O)