From ce87b6c6713d551b3700ea9732cd0955a3819952 Mon Sep 17 00:00:00 2001 From: Lynn Date: Thu, 21 Nov 2019 17:24:10 +0800 Subject: [PATCH] ddl: fix the error message for dropping primary key (#13665) --- ddl/ddl_api.go | 3 ++- ddl/serial_test.go | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 764bcfc242..7d0c7e1dd6 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -3582,7 +3582,8 @@ func (d *ddl) dropIndex(ctx sessionctx.Context, ti ast.Ident, isPK bool, indexNa return ErrUnsupportedModifyPrimaryKey.GenWithStack("Unsupported drop primary key when alter-primary-key is false") } - if indexInfo == nil { + // If the table's PKIsHandle is true, we can't find the index from the table. So we check the value of PKIsHandle. + if indexInfo == nil && !t.Meta().PKIsHandle { return ErrCantDropFieldOrKey.GenWithStack("Can't DROP 'PRIMARY'; check that column/key exists") } if t.Meta().PKIsHandle { diff --git a/ddl/serial_test.go b/ddl/serial_test.go index f1e2c0ce61..d12cd0e8a3 100644 --- a/ddl/serial_test.go +++ b/ddl/serial_test.go @@ -91,6 +91,26 @@ func (s *testSerialSuite) TestPrimaryKey(c *C) { c.Assert(ddl.ErrUnsupportedModifyPrimaryKey.Equal(err), IsTrue) _, err = tk.Exec("alter table primary_key_test drop primary key") c.Assert(err.Error(), Equals, "[ddl:8200]Unsupported drop primary key when alter-primary-key is false") + + // Change the value of AlterPrimaryKey. + tk.MustExec("create table primary_key_test1 (a int, b varchar(10), primary key(a))") + tk.MustExec("create table primary_key_test2 (a int, b varchar(10), primary key(b))") + tk.MustExec("create table primary_key_test3 (a int, b varchar(10))") + cfg := config.GetGlobalConfig() + newCfg := *cfg + orignalAlterPrimaryKey := newCfg.AlterPrimaryKey + newCfg.AlterPrimaryKey = true + config.StoreGlobalConfig(&newCfg) + defer func() { + newCfg.AlterPrimaryKey = orignalAlterPrimaryKey + config.StoreGlobalConfig(&newCfg) + }() + + _, err = tk.Exec("alter table primary_key_test1 drop primary key") + c.Assert(err.Error(), Equals, "[ddl:8200]Unsupported drop primary key when the table's pkIsHandle is true") + tk.MustExec("alter table primary_key_test2 drop primary key") + _, err = tk.Exec("alter table primary_key_test3 drop primary key") + c.Assert(err.Error(), Equals, "[ddl:1091]Can't DROP 'PRIMARY'; check that column/key exists") } func (s *testSerialSuite) TestMultiRegionGetTableEndHandle(c *C) {