-
-
Notifications
You must be signed in to change notification settings - Fork 514
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
Feature suggestion: LAST_INSERT_UUID() #7547
Comments
This is a good feature request. I think it's fine for us to extend MySQL. We'll have someone implement this function today. Likely @fulghum. |
Thanks for the great feature request @gitasmus! 🙏 I'm getting started on this one now for you. |
Quick update... I've got something rough working for this. The code for this is a little different from There's a design question around whether nested expressions using I'll keep working on getting this cleaned up, finishing the tests, and getting a PR out. |
Hey @fulghum , thank you for your fast response and work on this topic. I'm working with @gitasmus and we are very impressed by how fast you guys react to our issues. I want to add that using complex expressions is really common when using Would it be possible to just store the last generated UUID (e.g. in a session variable) whenever UUID() is called? This of course wouldn't necessarily only apply to inserts, so maybe you could name the procedure something like Another idea I had, was to supply a second procedure to generate UUIDs directly as a |
Thanks for the feedback @hannasty. It's very helpful to hear more about how you're using the I like your idea of keeping this really simple and just having the Instead of a new function, I'm picturing just directly accessing the session variable, something like this: insert into t values (UUID(), "data");
select @@last_generated_uuid; I'll get started building that out and seeing how it looks. |
I implemented this new approach today and spent some time chatting with a teammate about it. We like this feature idea, and we're debating the best interface to expose this information. One advantage of the session variable approach is that it has a very simple implementation. The biggest disadvantage we see is that it's not the same contract as I'll keep discussing the different approaches with the team. If you have thoughts/opinions, please share, it'd be very helpful for us to make sure we build the right thing for you. For example, it'd be helpful to hear if the callout on multiple |
Part of the issue here is that MySQL doesn't have an actual UUID type. Typically you store them as either the string representation ( CREATE TABLE uuid_bytes(
id BINARY(16) DEFAULT (UUID_TO_BIN(UUID())),
some_text VARCHAR(255),
PRIMARY KEY (id)
); CREATE TABLE uuid_varchar(
id CHAR(36) DEFAULT (UUID()),
some_text VARCHAR(255),
PRIMARY KEY (id)
); The idea would be that if your table has a single primary key column matching the type and default of one of the above column definitions, then Does that match your expectations for how such a feature should work? |
In our case it would be no problem because we are generating UUIDs only for primary keys. But as you mentioned you could use
Yes, absolutely. I think this solution would perfectly solve the issue mentioned above. Also it's completely fine that the return type would always be a string as long as the actual column could be a Thank you guys for your work on this! |
Thanks @hannasty. I've got dolthub/go-mysql-server#2362 opened up for review now, with the We'll get this reviewed and released and keep you updated. |
Just a quick update that we're getting really close on this one. We found an interesting edge case for |
We just released Dolt 1.35.1, which contains the new I'll update our docs to include this function and we'll probably write a quick blog for it in the next month or so, too. Until then, you can also find the example usage and requirements for auto UUID columns in the 1.35.1 release notes. Let us know if you have any issues using this new function or if there's anything else we can do to help you build with Dolt! |
In his blog Tim pointed out the usefulness of UUIDs: https://www.dolthub.com/blog/2023-10-27-uuid-keys/
A minimal example could look as follows. Create table with a UUID as default primary key:
Next Insert a row without giving an ID:
If we are interested in the last inserted ID
SELECT LAST_INSERT_ID();
does not return the UUID as it's return type is hard-wired to
UNSIGNED BIGINT
.In analogy to
LAST_INSERT_ID()
it would thus be nice if dolt provided a feature that is also missing in MySQL:LAST_INSERT_UUID()
, which would retur the last auto-created UUID.The lack of such a
LAST_INSERT_UUID()
function could be circumvented in MySQL and dolt with aTRIGGER
as described inhttps://stackoverflow.com/questions/4687196/mysql-insert-id-or-something-like-that-to-return-last-mysql-uuid
A dedicated
LAST_INSERT_UUID()
function in dolt would IMHO be appropriate to the importance of UUIDs (and convenient), but would break the compatibility with MySQL. The alternative of implementing the return type ofLAST_INSERT_ID()
in a more general way to allow forID
to beUNSIGNED BIGINT
,UUID()
to beVARCHAR(36)
andUUID_TO_BIN(UUID())
to beBINARY(16)
could be even more logical but would cause even more compatibility confusion with MySQL.Perhaps, it would be the better approach to ask for such a feature in MySQL first and then ask for the compatibility of dolt here ...
The text was updated successfully, but these errors were encountered: