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

Support ULID #13679

Open
p6l-richard opened this issue Jun 7, 2022 · 15 comments
Open

Support ULID #13679

p6l-richard opened this issue Jun 7, 2022 · 15 comments
Labels
domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. domain/psl Issue in the "PSL" domain: Prisma Schema Language domain/schema Issue in the "Schema" domain: Prisma Schema, Introspection, Migrations etc. kind/feature A request for a new feature. topic: default topic: uuid

Comments

@p6l-richard
Copy link

Problem

UUID can be suboptimal for many use-cases because:

It isn't the most character efficient way of encoding 128 bits of randomness
UUID v1/v2 is impractical in many environments, as it requires access to a unique, stable MAC address
UUID v3/v5 requires a unique seed and produces randomly distributed IDs, which can cause fragmentation in many data structures
UUID v4 provides no other information than randomness which can cause fragmentation in many data structures

From the ULID spec.

The Lexicographically sortability would especially be useful for cursor-based pagination which requires sorting by a unique, sequential column.

Suggested solution

Support ULID in the prisma schema, by defining:
@db.Ulid

@jkomyno jkomyno added kind/feature A request for a new feature. domain/schema Issue in the "Schema" domain: Prisma Schema, Introspection, Migrations etc. topic: uuid labels Jun 8, 2022
@janpio janpio added domain/psl Issue in the "PSL" domain: Prisma Schema Language topic: default domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. labels Jun 9, 2022
@mbrookson
Copy link

mbrookson commented Sep 10, 2022

It would be great to see this feature supported!

Since ULID is designed to be the same 128-bits as UUID it should work with the underlying database type, but using this means Prisma throws the error because it is validating the string length which is only 26 characters for ULID.

From some further research into this however it seems that Postgres itself doesn't really support ULID as a datatype yet and it's difficult to achieve. Maybe something to consider for the future if this does ever become a feature.

@rafaell-lycan
Copy link

rafaell-lycan commented Sep 25, 2022

I've found this thread as my company uses ULID via a Golang lib and generates the @id on the backend instead of relying on the DB and now I was searching for a way to do the same with Prisma (my ORM of choice).

I've asked about CUID and the backends have answered they did research but options and ULID was the "best option at that time" (~5-6 years ago)

@matus-sabo
Copy link

matus-sabo commented Oct 29, 2022

In postgres you can create function to generate ULID (uuid v7 or uuid v8) https://gist.github.com/kjmph/5bd772b2c2df145aa645b837da7eca74

@ikeyan
Copy link

ikeyan commented Jun 9, 2023

@matus-sabo that's cool, but it's UUID, not ULID.

@pksunkara
Copy link

I am the author of https://github.com/pksunkara/pgx_ulid and I would be willing to implement this. @janpio and prima people, do I have the go-ahead?

@janpio
Copy link
Member

janpio commented Aug 9, 2023

What exactly is even needed here if there is a PostgreSQL extension?
Is https://www.prisma.io/docs/concepts/components/prisma-schema/postgresql-extensions not enough?

@pksunkara
Copy link

But @db.Ulid is not supported in the schema types though?

@janpio
Copy link
Member

janpio commented Aug 10, 2023

Ah, I see that the extension also adds a new ulid type in PostgreSQL, additionally to the function to create one?
Can a generated value only be stored in such a ulid column, or also as a normal text string?

Currently Prisma does not offer a way for extensions to add native types.

@pksunkara
Copy link

pksunkara commented Aug 10, 2023

Can a generated value only be stored in such a ulid column, or also as a normal text string?

Yes, a generated value can only be stored in such an ulid column. It is visible to end-users as a string, but internally it's stored in a much more compact format which is why a new type was necessary.

Currently Prisma does not offer a way for extensions to add native types.

Yup, which is why I was offering to create a native type Ulid in Prisma query engine. All I need is someone to point me to the specific part of the code.

@janpio
Copy link
Member

janpio commented Aug 10, 2023

Yup, which is why I was offering to create a native type Ulid in Prisma query engine. All I need is someone to point me to the specific part of the code.

Ulid is not available on most PostgreSQL databases, only when you install this extension. So we could not merge such an addition of a native type, unless it also depends on the extension being used in the schema. We do not have this mechanism yet at all.

@pksunkara
Copy link

I guess the only way to solve this would be use a custom type as specified in #5039? But there's nothing happening on that.

@mbrookson
Copy link

Since ULID is designed to be the same 128-bits as UUID it should work with the underlying database type, but using this means Prisma throws the error because it is validating the string length which is only 26 characters for ULID.

I could well be wrong, but referring to previous comment, if ULID is 128-bits can it not just be stored in a standard UUID column?

@pksunkara
Copy link

It is actually stored in the same format, but since it needs to be read differently, that is why a new type is needed.

@Falmarri
Copy link

Are ULIDs still needed now that v7 UUIDs are coming into the spec? Should probably just use that.

@kibertoad
Copy link

I'm linking #24079 here, as it aims to address the same problem in a slightly different way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. domain/psl Issue in the "PSL" domain: Prisma Schema Language domain/schema Issue in the "Schema" domain: Prisma Schema, Introspection, Migrations etc. kind/feature A request for a new feature. topic: default topic: uuid
Projects
None yet
Development

No branches or pull requests

10 participants