Created
December 15, 2025 00:47
-
-
Save kenn/aeb293866ca975b0caf1b9be20f52951 to your computer and use it in GitHub Desktop.
Drizzle primaryKey().generatedByDefaultAsIdentity()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export const users = pgTable( | |
| 'users', | |
| { | |
| id: integer('id').primaryKey().generatedByDefaultAsIdentity() | |
| } | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- 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