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

dml : fix last insert id when autoid specified by user in first row. (#11973) #12002

Merged
merged 2 commits into from
Sep 4, 2019
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
5 changes: 3 additions & 2 deletions executor/insert_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,9 @@ func (e *InsertValues) adjustAutoIncrementDatum(ctx context.Context, d types.Dat
if e.filterErr(err) != nil {
return types.Datum{}, err
}
// It's compatible with mysql. So it sets last insert id to the first row.
if e.rowCount == 1 {
// It's compatible with mysql setting the first allocated autoID to lastInsertID.
// Cause autoID may be specified by user, judge only the first row is not suitable.
if e.lastInsertID == 0 {
e.lastInsertID = uint64(recordID)
}
}
Expand Down
16 changes: 16 additions & 0 deletions executor/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,22 @@ func (s *testSuite3) TestInsertWithAutoidSchema(c *C) {
`select * from t1 where id = 9`,
testkit.Rows(`9 9`),
},
// test last insert id
{
`insert into t1 values(3000, -1), (null, -2)`,
`select * from t1 where id = 3000`,
testkit.Rows(`3000 -1`),
},
{
`;`,
`select * from t1 where id = 3001`,
testkit.Rows(`3001 -2`),
},
{
`;`,
`select last_insert_id()`,
testkit.Rows(`3001`),
},
{
`insert into t2(id, n) values(1, 1)`,
`select * from t2 where id = 1`,
Expand Down