cloudflare / cloudflare/developer-platform

D1 doesn't include enough information when foreign key constraints fail

Open
#6 5 comments 6 reactions 0 assignees View on GitHub
product:d1
Dominant language
No language data
Stars
1
Forks
0
PR merge metrics
No merged PRs in 30d

Description

I have this Drizzle schema
```ts
export const users = sqliteTable(
"users",
{
id: text("id").primaryKey(),
name: text("name").notNull(),
// ...
})

export const accounts = sqliteTable(
"accounts",
{
id: text("id").primaryKey(),
userId: text("userId")
.notNull()
.references(() => users.id),
// ...
})
```

Which produces this migration
```sql
CREATE TABLE `users` (
`id` text PRIMARY KEY NOT NULL,
`name` text NOT NULL
);

CREATE TABLE `accounts` (
`id` text PRIMARY KEY NOT NULL,
`accountId` text NOT NULL,
`userId` text NOT NULL,
FOREIGN KEY (`userId`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action
);
```

When I try to delete a user that has accounts, I get this error:
```ts
SqliteError: FOREIGN KEY constraint failed
at proxy (/home/vlady/code/gyarns/node_modules/.pnpm/drizzle-kit@0.30.2/node_modules/drizzle-kit/bin.cjs:64941:47)
at /home/vlady/code/gyarns/node_modules/.pnpm/drizzle-kit@0.30.2/node_modules/drizzle-kit/bin.cjs:74841:32
at dispatch (/home/vlady/code/gyarns/node_modules/.pnpm/drizzle-kit@0.30.2/node_modules/drizzle-kit/bin.cjs:72483:27)
at /home/vlady/code/gyarns/node_modules/.pnpm/drizzle-kit@0.30.2/node_modules/drizzle-kit/bin.cjs:72484:24
at /home/vlady/code/gyarns/node_modules/.pnpm/drizzle-kit@0.30.2/node_modules/drizzle-kit/bin.cjs:71944:15
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async dispatch (/home/vlady/code/gyarns/node_modules/.pnpm/drizzle-kit@0.30.2/node_modules/drizzle-kit/bin.cjs:72483:21)
at async cors2 (/home/vlady/code/gyarns/node_modules/.pnpm/drizzle-kit@0.30.2/node_modules/drizzle-kit/bin.cjs:73520:9)
at async dispatch (/home/vlady/code/gyarns/node_modules/.pnpm/drizzle-kit@0.30.2/node_modules/drizzle-kit/bin.cjs:72483:21)
at async /home/vlady/code/gyarns/node_modules/.pnpm/drizzle-kit@0.30.2/node_modules/drizzle-kit/bin.cjs:74793:9 {
code: 'SQLITE_CONSTRAINT_FOREIGNKEY'
}
```
I get the same `FOREIGN KEY constraint failed` error in the D1 dashboard when I use the GUI to delete a user with an account. These errors are hard to debug in big schemas because they don't say exactly which constraint failed.

How can we include a name of the constraint in the error?

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.