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

knex should use binary(16) for UUID outside of PG #1778

Closed
ItalyPaleAle opened this issue Nov 9, 2016 · 18 comments · Fixed by #4836
Closed

knex should use binary(16) for UUID outside of PG #1778

ItalyPaleAle opened this issue Nov 9, 2016 · 18 comments · Fixed by #4836

Comments

@ItalyPaleAle
Copy link

Currently, on databases other than PostgreSQL, knex is creating UUID tables as char(36), wasting a lot of space (and making it inconvenient to use those columns as primary keys).

knex should store UUIDs as binary(16) instead, as each UUID is a 128bit (=16 byte) number and it's only represented with 36 hex characters for convenience.

@PlasmaPower
Copy link

PlasmaPower commented Nov 22, 2016

I've stumbled upon this issue while researching a slightly related topic: do binary fields even work in knex? If using the postgres backend, it would appear they are at least somewhat broken: brianc/node-postgres#738.

Are binary fields ready for prod use, or should I stick to hex strings for now?

@PlasmaPower
Copy link

We should have tests for using binary fields (currently we just have tests for creating them it seems). I would expect them to work fully with node Buffers across SQL backends.

@ItalyPaleAle
Copy link
Author

I've had success with passing Buffer objects for binary data (with MySQL), but I'm still in the early test stages.

@PlasmaPower
Copy link

Interesting. That might be a good reason for me to use the MySQL backend, but I'd prefer to not be locked into it...

@isocroft
Copy link

isocroft commented Jul 4, 2019

I can work on this and send a PR if it's okay... i also need this too ( binary(16) ) columns

@kibertoad
Copy link
Collaborator

Makes sense for me. @elhigu @lorefnon Thoughts?
This would be a breaking change, though.

@kibertoad
Copy link
Collaborator

One possible workaround to make it non-breaking would be to introduce new column type binaryUuid (that would work exactly same as UUID for postgres) so that it's an opt-in functionality.

@elhigu
Copy link
Member

elhigu commented Jul 4, 2019

This needs to be done backwards compatible way with new opts parameter.

Generally there should not be made breaking changes to migration APIs since they are super dangerous and nasty to migrate.

@isocroft
Copy link

isocroft commented Jul 4, 2019

Ok... so i go ahead with the non-breaking binaryUuid option as @kibertoad suggests OR do i create an additional param on existing uuid() table object method as @elhigu suggests ?

Folks could still do this :

specificType('id', 'binary(16)')

however, don't want to introduce some irrelevance to knex just because i wish to support MySQL binary(16)

@kibertoad
Copy link
Collaborator

kibertoad commented Jul 4, 2019

@isocroft If I understand @elhigu idea correctly, he is suggesting to extend table.uuid(name) to be table.uuid(name, opts), where opts would have something like 'useBinaryUuid' parameter that is defaulted to false unless on postgres.

@isocroft
Copy link

isocroft commented Jul 4, 2019

Yes, i do agree with @elhigu on this approach it is safe and will be backward compatible above all.

So, i will send PR as soon as i get a chance over the weekend... Cheers!

@edgarbjorntvedt
Copy link

What is the status on this?

@grefrit
Copy link

grefrit commented Nov 18, 2021

Any updates or workarounds?? We use binary uuids in our project too with directus and we need to patch knex through yarn patch to fix this issue. Maybe some thoughts how to make binary uuids work without some scary patches

@OlivierCavadenti
Copy link
Collaborator

Any updates or workarounds?? We use binary uuids in our project too with directus and we need to patch knex through yarn patch to fix this issue. Maybe some thoughts how to make binary uuids work without some scary patches

I will push something quickly.

@OlivierCavadenti
Copy link
Collaborator

@grefrit I push, you can check at #4836 if all is ok for you.

@grefrit
Copy link

grefrit commented Nov 18, 2021

@OlivierCavadenti Awesome, thank you a lot. But I didn't understand in your PR, how UUID selects or updates. It should convert to normal UUID when you SELECT something, and when u UPDATE it should to convert from normal UUID to binary(16), because you won't use binary(16) on website for example. Or I misunderstood something :)?

@OlivierCavadenti
Copy link
Collaborator

OlivierCavadenti commented Nov 18, 2021

@OlivierCavadenti Awesome, thank you a lot. But I didn't understand in your PR, how UUID selects or updates. It should convert to normal UUID when you SELECT something, and when u UPDATE it should to convert from normal UUID to binary(16), because you won't use binary(16) on website for example. Or I misunderstood something :)?

Ok and I see the other same comment in my PR. I will check what you talking about :D.

@kibertoad
Copy link
Collaborator

Released in 1.0.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants