drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
23.25k stars 565 forks source link

[FEATURE]: Allow for non sequential primary key for id of migrationsTable #1267

Open aydrian opened 12 months ago

aydrian commented 12 months ago

Describe what you want

For at least the pg-core dialect, it would be great if we had the option to set the id column type to something non sequential like a UUID. I have been trying Drizzle with CockroachDB and everything seems to be working okay but it does complain about the SERIAL id type on the migrationsTable. Sequential primary keys in distributed databases can cause hot spotting. CockroachDB recommends using a UUID with a default value of gen_random_uuid(). I don't think this will cause a problem in the long run but it would remove the warning when running a migration.

AndriiSherman commented 11 months ago

You can use uuid field in pg-core, I guess it's just missing in docs, will fix it!

import { pgTable, text, uuid } from "drizzle-orm/pg-core";

export const users = pgTable("users", {
    id: uuid("uuid1").defaultRandom(),
    name: text("name"),
});

Would that work for you?

Also you can use .default for any sql defaults, so you can write like this:

import { pgTable, text, uuid } from "drizzle-orm/pg-core";

export const users = pgTable("users", {
    id: uuid("uuid1").default(sql`gen_random_uuid()`),
    name: text("name"),
});
AndriiSherman commented 11 months ago

We have an example here: https://orm.drizzle.team/docs/column-types/pg#default-value Just never made it in a list of types, just created an issue in docs repo: https://github.com/drizzle-team/drizzle-orm-docs/issues/120

aydrian commented 11 months ago

I'm not referring to the tables I create in my schema, but rather the table Drizzle creates to hold information about migrations. It uses a serial type for the primary key.

AndriiSherman commented 11 months ago

makes sense, assigning to myself

aydrian commented 11 months ago

Just ran into another issue using push/migration after I added a foreign key to my schema. Looks like we don't yet support anonymous code blocks. We do have a ticket for it.

DO $$ BEGIN
 ALTER TABLE "count_downs" ADD CONSTRAINT "count_downs_id_lists_id_fk" FOREIGN KEY ("id") REFERENCES "lists"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;