From 6419ff6fb1ce737367e8e2a43149900a77326325 Mon Sep 17 00:00:00 2001 From: Tanner Date: Sun, 29 Sep 2019 00:31:31 +0800 Subject: [PATCH] ddl: allow multiple `unique` attributes in a column (#12165) --- ddl/db_integration_test.go | 14 ++++++++++++++ ddl/ddl_api.go | 18 ++++++++++++------ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index 0c259f637fa94..5eda083244dfc 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -923,3 +923,17 @@ func (s *testIntegrationSuite) TestDropAutoIncrement(c *C) { tk.MustExec("alter table t1 modify column a int") tk.MustExec("set @@tidb_allow_remove_auto_inc = off") } + +func (s *testIntegrationSuite) TestMultipleUnique(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("create database if not exists test") + tk.MustExec("use test") + + tk.MustExec("drop table if exists multi_unique") + tk.MustExec("create table multi_unique (a int unique unique)") + tk.MustExec("drop table multi_unique") + tk.MustExec("create table multi_unique (a int key primary key unique unique)") + tk.MustExec("drop table multi_unique") + tk.MustExec("create table multi_unique (a int key unique unique key unique)") + tk.MustExec("drop table multi_unique") +} diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 6c55317e14b44..7b7dedd245b2c 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -496,13 +496,19 @@ func columnDefToCol(ctx sessionctx.Context, offset int, colDef *ast.ColumnDef, o case ast.ColumnOptionAutoIncrement: col.Flag |= mysql.AutoIncrementFlag case ast.ColumnOptionPrimaryKey: - constraint := &ast.Constraint{Tp: ast.ConstraintPrimaryKey, Keys: keys} - constraints = append(constraints, constraint) - col.Flag |= mysql.PriKeyFlag + // Check PriKeyFlag first to avoid extra duplicate constraints. + if col.Flag&mysql.PriKeyFlag == 0 { + constraint := &ast.Constraint{Tp: ast.ConstraintPrimaryKey, Keys: keys} + constraints = append(constraints, constraint) + col.Flag |= mysql.PriKeyFlag + } case ast.ColumnOptionUniqKey: - constraint := &ast.Constraint{Tp: ast.ConstraintUniqKey, Name: colDef.Name.Name.O, Keys: keys} - constraints = append(constraints, constraint) - col.Flag |= mysql.UniqueKeyFlag + // Check UniqueFlag first to avoid extra duplicate constraints. + if col.Flag&mysql.UniqueFlag == 0 { + constraint := &ast.Constraint{Tp: ast.ConstraintUniqKey, Keys: keys} + constraints = append(constraints, constraint) + col.Flag |= mysql.UniqueKeyFlag + } case ast.ColumnOptionDefaultValue: hasDefaultValue, err = setDefaultValue(ctx, col, v) if err != nil {