drizzle-team / drizzle-team/drizzle-orm

Composite pk always causes drizzle-kit push to crash.

Open
#3,342 17 comments 23 reactions 0 assignees View on GitHub
db/postgres drizzle/kit priority
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

The following table (from Auth.js)
```ts
export const authenticators = pgTable(
"authenticator",
{
credentialID: text("credentialID").notNull().unique(),
userId: text("userId")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
providerAccountId: text("providerAccountId").notNull(),
credentialPublicKey: text("credentialPublicKey").notNull(),
counter: integer("counter").notNull(),
credentialDeviceType: text("credentialDeviceType").notNull(),
credentialBackedUp: boolean("credentialBackedUp").notNull(),
transports: text("transports"),
},
(table) => {
return {
pk: primaryKey({
name: 'authenticator_userid_credentialid_pk',
columns: [table.userId, table.credentialID],
})
}
}
);
```
Always causes `drizzle-kit push` to crash with the following error:
```shell
srv/lib/../../db❱ pnpm drizzle-kit push 
drizzle-kit: v0.22.7
drizzle-orm: v0.31.2

No config path provided, using default path
Reading config file 'C:\srv\lib\tilly\packages\db\drizzle.config.ts'
Using 'pg' driver for database querying
[✓] Pulling schema from database...

Warning You are about to execute current statements:

ALTER TABLE "authenticator" DROP CONSTRAINT "authenticator_userid_credentialid_pk";

ALTER TABLE "Topic" ALTER COLUMN "number" SET DEFAULT '1';
DO $$ BEGIN
ALTER TABLE "authenticator" ADD CONSTRAINT "authenticator_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;

ALTER TABLE "authenticator" DROP CONSTRAINT authenticator_userId_credentialID_pk;
--> statement-breakpoint
ALTER TABLE "authenticator" ADD CONSTRAINT authenticator_userId_credentialID_pk PRIMARY KEY(userId,credentialID);

error: constraint "authenticator_userid_credentialid_pk" of relation "authenticator" does not exist
at C:\srv\lib\tilly\node_modules\.pnpm\drizzle-kit@0.22.7\node_modules\drizzle-kit\bin.cjs:77696:15
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Object.query (C:\srv\lib\tilly\node_modules\.pnpm\drizzle-kit@0.22.7\node_modules\drizzle-kit\bin.cjs:119635:26)
at async pgPush (C:\srv\lib\tilly\node_modules\.pnpm\drizzle-kit@0.22.7\node_modules\drizzle-kit\bin.cjs:122560:13)
at async _Command. (C:\srv\lib\tilly\node_modules\.pnpm\drizzle-kit@0.22.7\node_modules\drizzle-kit\bin.cjs:129681:7) {
length: 162,
severity: 'ERROR',
code: '42704',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'tablecmds.c',
line: '12070',
routine: 'ATExecDropConstraint'
}
```
Running the drop constraint command manually works (and afterwards the `drizzle-kit push` command works again - which is quite the hazzle):
![image](https://github.com/drizzle-team/drizzle-orm/assets/1193143/d937525a-3cbc-4a73-a0cc-aeda5cf35769)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.