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

tables: make duplicate error in update clearer #7495

Merged
merged 7 commits into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 12 additions & 0 deletions executor/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ func (s *testSuite) TestInsertOnDuplicateKey(c *C) {
tk.MustQuery(`select * from t1`).Check(testkit.Rows("1 400"))
}

func (s *testSuite) TestUpdateDuplicateKey(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")

tk.MustExec(`drop table if exists t;`)
tk.MustExec(`create table c(i int,j int,k int,primary key(i,j,k));`)
tk.MustExec(`insert into c values(1,2,3);`)
tk.MustExec(`insert into c values(1,2,4);`)
_, err := tk.Exec(`update c set i=1,j=2,k=4 where i=1 and j=2 and k=3;`)
c.Assert(err.Error(), Equals, "[kv:1062]Duplicate entry '1-2-4' for key 'PRIMARY'")
}

func (s *testSuite) TestInsertWrongValueForField(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
2 changes: 1 addition & 1 deletion executor/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ func (s *testSuite) TestUpdate(c *C) {
_, err = tk.Exec("update ignore t set a = 1 where a = 2;")
c.Assert(err, IsNil)
r = tk.MustQuery("SHOW WARNINGS;")
r.Check(testkit.Rows("Warning 1062 key already exist"))
r.Check(testkit.Rows("Warning 1062 Duplicate entry '1' for key 'I_uniq'"))
tk.MustQuery("select * from t").Check(testkit.Rows("1", "2"))

tk.MustExec("drop table if exists t")
Expand Down
10 changes: 10 additions & 0 deletions table/tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,16 @@ func (t *tableCommon) removeRowIndex(sc *stmtctx.StatementContext, rm kv.Retriev
// buildIndexForRow implements table.Table BuildIndexForRow interface.
func (t *tableCommon) buildIndexForRow(ctx sessionctx.Context, rm kv.RetrieverMutator, h int64, vals []types.Datum, idx table.Index) error {
if _, err := idx.Create(ctx, rm, vals, h); err != nil {
if kv.ErrKeyExists.Equal(err) {
// Make error message consistent with MySQL.
entryKey, err1 := t.genIndexKeyStr(vals)
if err1 != nil {
// if genIndexKeyStr failed, return the original error.
return errors.Trace(err)
}

return kv.ErrKeyExists.FastGen("Duplicate entry '%s' for key '%s'", entryKey, idx.Meta().Name)
}
return errors.Trace(err)
}
return nil
Expand Down