Can someone direct me to a UUID implementation as PK with Drizzle ORM?
Hello eveyone. I'm trying to create a user table in a Vercel Postgres db, but keep running into errors.
Could anyone kindly provide guidance,? An example would be perfect.
I'm looking for a schema of sorts.
Any help will be greatly appreciated!
15 Replies
Try something like this:
@Blixkreeg , thanks. It's appreciated.
Bbut now I'm having basic configuration problems. I have psoted a new question to see if I can get any help.
@Blixkreeg , your schema update gets me this error:
error: column "id" cannot be cast automatically to type uuid
(...) length: 169,
severity: 'ERROR',
code: '42804',
detail: undefined,
hint: 'You might need to specify "USING id::uuid".',
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'tablecmds.c',
line: '12336',
routine: 'ATPrepAlterColumnType'
}
you're using a postgres database? And have you been able to run any migrations at all?
@Blixkreeg , Yes, I have now been able to run migrations sucessfully. The problem seems to b restricted to UUIDs, at least for now
ok i see. is this error coming from running a query?
Have you made sure you generated your migrations and migrated after every change?
IIRC, this showed up after genereate and migrate
I am having the same issue. I am able to run
generate:pg
and push:pg
without issue using code like you gave @Blixkreeg .
However, when I use the example code from the running migrations docs I get an error, same as @nr7751 did.Drizzle ORM - Overview
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
And this is the migrate script (
src/lib/db/db_migrate.server.ts
):
@Peter Boling , I solved it uisng cuid2
import { createId } from "@paralleldrive/cuid2";
and in the table:
export const users = pgTable(
"users",
{
id: text("id")
.$defaultFn(() => createId())
.primaryKey(),
...
Hope this helps
I specifically am attempting to avoid using a text field as that bloats the size of the key immensely. I want it to be binary.
@Peter Boling , but then you can't use any form of UUID , can you?
uuid
is a native binary type with the postgresql extension uuid-ossp
, but it only ships with a generator for UUID v1, v3, v4, and v5
I want to use either ulid
or uuidv7
uuidv7 is binary format compatible with the older uuid formats, so I can store a uuidv7 inside a uuid field from uuid-ossp
The issue is that Drizzle + Lucia doesn't allow me to override the type of the session id (they only just added the ability to override the type of the user id).
I did end up getting everything working, except for the session id. All my other IDs are now uuid, and they are v7 when generated by Javascript clients, and v4 when generated by the database.Intersting. I will be implementing auth with Lucia as well.
Any considerations I should eb aware of?
WOuld you say my implementation might cause problems with Lucia?
No, if you are using text it will just work 🙂
Probably best to start there, the performance difference won't matter until you hit millions of users!
@nr7751 I described my solution over on the Lucia discord: https://discord.com/channels/1004048134218981416/1219330483796574270/1219342383918092318
i use it like this:
i also tried using
sqlite
extension using ulid but it wasn't working for some reason. https://github.com/asg017/sqlite-ulid/issues/9