Skip to content

Instantly share code, notes, and snippets.

@kenn
Created December 15, 2025 00:47
Show Gist options
  • Select an option

  • Save kenn/aeb293866ca975b0caf1b9be20f52951 to your computer and use it in GitHub Desktop.

Select an option

Save kenn/aeb293866ca975b0caf1b9be20f52951 to your computer and use it in GitHub Desktop.
Drizzle primaryKey().generatedByDefaultAsIdentity()
-- users
ALTER TABLE "users" ALTER COLUMN "id" DROP DEFAULT;--> statement-breakpoint
ALTER SEQUENCE "users_id_seq" RENAME TO "users_id_seq_old";--> statement-breakpoint
ALTER TABLE "users" ALTER COLUMN "id" ADD GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1);--> statement-breakpoint
SELECT setval('users_id_seq', (SELECT GREATEST(COALESCE(MAX(id), 1), 1) FROM "users"), (SELECT COALESCE(MAX(id), 0) >= 1 FROM "users"));--> statement-breakpoint
DROP SEQUENCE "users_id_seq_old";--> statement-breakpoint
export const users = pgTable(
'users',
{
id: integer('id').primaryKey().generatedByDefaultAsIdentity()
}
)
-- To verify the identity columns, you can run the following query:
SELECT is_identity, identity_generation, column_default
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'users'
AND column_name = 'id';
-- should be `is_identity = YES` and `identity_generation = BY DEFAULT`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment