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

Research direction

Begin with the TypeScript schema and generated SQLite migration shown in the report, then reproduce the failing user deletion in the D1 dashboard. The payload names no repository files or tests; done means the foreign-key failure identifies the specific constraint involved instead of only reporting a generic error.

Written by the indexing model from the issue text.

Assessment

Tech stack
sqlite, typescript
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.