Skip to content

Commit

Permalink
ddl: fix add a PK when the table's pk_is_handle is true (#18340) (#18342
Browse files Browse the repository at this point in the history
)

Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
ti-srebot authored Jul 8, 2020
1 parent 666be7c commit 8062359
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3288,7 +3288,9 @@ func (d *ddl) CreatePrimaryKey(ctx sessionctx.Context, ti ast.Ident, indexName m
}

indexName = model.NewCIStr(mysql.PrimaryKeyName)
if indexInfo := t.Meta().FindIndexByName(indexName.L); indexInfo != nil {
if indexInfo := t.Meta().FindIndexByName(indexName.L); indexInfo != nil ||
// If the table's PKIsHandle is true, it also means that this table has a primary key.
t.Meta().PKIsHandle {
return infoschema.ErrMultiplePriKey
}

Expand Down
8 changes: 8 additions & 0 deletions ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ func (s *testSerialSuite) TestPrimaryKey(c *C) {
config.StoreGlobalConfig(&newCfg)
}()

_, err = tk.Exec("alter table primary_key_test2 add primary key(a)")
c.Assert(infoschema.ErrMultiplePriKey.Equal(err), IsTrue)
// We can't add a primary key when the table's pk_is_handle is true.
_, err = tk.Exec("alter table primary_key_test1 add primary key(a)")
c.Assert(infoschema.ErrMultiplePriKey.Equal(err), IsTrue)
_, err = tk.Exec("alter table primary_key_test1 add primary key(b)")
c.Assert(infoschema.ErrMultiplePriKey.Equal(err), IsTrue)

_, err = tk.Exec("alter table primary_key_test1 drop primary key")
c.Assert(err.Error(), Equals, "[ddl:206]Unsupported drop primary key when the table's pkIsHandle is true")
tk.MustExec("alter table primary_key_test2 drop primary key")
Expand Down

0 comments on commit 8062359

Please sign in to comment.