Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddl: fix add a PK when the table's pk_is_handle is true (#18340) #18342

Merged
merged 2 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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