diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index f2a8b6b924..6c8eeaf54c 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -3562,7 +3562,10 @@ func checkColumnWithIndexConstraint(tbInfo *model.TableInfo, originalCol, newCol func checkAutoRandom(tableInfo *model.TableInfo, originCol *table.Column, specNewColumn *ast.ColumnDef) (uint64, error) { // Disallow add/drop actions on auto_random. - oldRandBits := tableInfo.AutoRandomBits + var oldRandBits uint64 + if tableInfo.PKIsHandle && (tableInfo.GetPkName().L == originCol.Name.L) { + oldRandBits = tableInfo.AutoRandomBits + } newRandBits, err := extractAutoRandomBitsFromColDef(specNewColumn) if err != nil { return 0, errors.Trace(err) diff --git a/ddl/serial_test.go b/ddl/serial_test.go index 9b34e4fd84..902d4c3c2d 100644 --- a/ddl/serial_test.go +++ b/ddl/serial_test.go @@ -1028,6 +1028,7 @@ func (s *testSerialSuite) TestAutoRandom(c *C) { // Add/drop the auto_random attribute is not allowed. mustExecAndDrop("create table t (a bigint auto_random(3) primary key)", func() { assertAlterValue("alter table t modify column a bigint") + assertAlterValue("alter table t modify column a bigint auto_random(0)") assertAlterValue("alter table t change column a b bigint") }) mustExecAndDrop("create table t (a bigint, b char, c bigint auto_random(3), primary key(c))", func() { @@ -1037,6 +1038,10 @@ func (s *testSerialSuite) TestAutoRandom(c *C) { mustExecAndDrop("create table t (a bigint primary key)", func() { assertAlterValue("alter table t modify column a bigint auto_random(3)") }) + mustExecAndDrop("create table t (a bigint, b bigint, primary key(a, b))", func() { + assertAlterValue("alter table t modify column a bigint auto_random(3)") + assertAlterValue("alter table t modify column b bigint auto_random(3)") + }) // Decrease auto_random bits is not allowed. mustExecAndDrop("create table t (a bigint auto_random(10) primary key)", func() { @@ -1064,10 +1069,13 @@ func (s *testSerialSuite) TestAutoRandom(c *C) { // Here the throw error is `ERROR 8200 (HY000): Unsupported modify column: length 11 is less than origin 20`, // instead of `ERROR 8216 (HY000): Invalid auto random: modifying the auto_random column type is not supported` // Because the origin column is `bigint`, it can not change to any other column type in TiDB limitation. - mustExecAndDrop("create table t (a bigint primary key auto_random(3))", func() { + mustExecAndDrop("create table t (a bigint primary key auto_random(3), b int)", func() { assertModifyColType("alter table t modify column a int auto_random(3)") assertModifyColType("alter table t modify column a mediumint auto_random(3)") assertModifyColType("alter table t modify column a smallint auto_random(3)") + tk.MustExec("alter table t modify column b int") + tk.MustExec("alter table t modify column b bigint") + tk.MustExec("alter table t modify column a bigint auto_random(3)") }) // Test show warnings when create auto_random table.