Skip to content

update LAST_INSERT_ID when auto incrementing from empty, NULL, and DEFAULT#2614

Merged
jycor merged 9 commits intomainfrom
james/auto
Aug 1, 2024
Merged

update LAST_INSERT_ID when auto incrementing from empty, NULL, and DEFAULT#2614
jycor merged 9 commits intomainfrom
james/auto

Conversation

@jycor
Copy link
Copy Markdown
Contributor

@jycor jycor commented Jul 31, 2024

Our logic for determining whether or not we needed to update last insert id only looked at the insertSource schema.
This does not take into consideration empty, NULL or DEFAULT values.
Additionally, the value that last insert id is set to depends on what the auto increment value will be.

This PR addresses those issues.
Also, has some refactoring for readability.

fixes: dolthub/dolt#7565

@jycor jycor changed the title have auto_increment with default update LAST_INSERT_ID when auto incrementing from empty, NULL, and DEFAULT Jul 31, 2024
Copy link
Copy Markdown
Contributor

@fulghum fulghum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job fixing this one!! 🙌

Only two real requests from me:

  • see if we can improve the naming a bit and make it really consistent across all the types where we're using autoAuto... stuff.
  • double check if the latest versions of MySQL really allow empty values for an auto_inc column.


{
Query: "insert into t(pk) values (10), (default);",
Expected: []sql.Row{{types.OkResult{RowsAffected: 2, InsertID: 10}}},
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(minor) should InsertID be 11? I couldn't see where MySQL was returning this field, but it seems like it would be the same as last_insert_id? (all I saw MySQL return was the RowsAffected)

Not a big deal, and I don't think we need to change it here, since it isn't really user facing, but figured I'd mention it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It definitely seems like it should be 11.
Fixing it breaks a bunch of tests, and I'm not sure if they are wrong or not.
Will look further into this in a future PR.

Comment on lines +2179 to +2182
{
Query: "insert into t(pk) values ();",
Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 26}}},
},
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In MySQL 8.3, I get an error for this one:

insert into t(pk) values ();
ERROR 1136 (21S01): Column count doesn't match value count at row 1

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I think this is actually an existing bug that's unrelated to auto_increment and any of the changes in this PR.
The test should be insert into t values (), which doesn't error in MySQL

Will make an issue for this case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jycor jycor merged commit 0a543f7 into main Aug 1, 2024
@jycor jycor deleted the james/auto branch August 1, 2024 20:19
@jycor jycor mentioned this pull request Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dolt doesn't update last_insert_id() when DEFAULT is used

2 participants