cloudflare / cloudflare/developer-platform

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

オープン
#6 コメント 5 件 リアクション 6 件 担当者 0 名 GitHub で見る
product:d1
主要言語
言語のデータがありません
スター
1
フォーク
0
PR マージ指標
30日以内にマージされた PR はありません

説明

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?

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

レポートに示されている TypeScript スキーマと生成された SQLite マイグレーションから始め、その後 D1 dashboard で失敗するユーザー削除を再現してください。payload ではリポジトリのファイルもテストも指定されていません。完了の条件は、foreign-key failure が関係する具体的な constraint を特定し、単に一般的なエラーを報告するだけではないことです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
sqlite, typescript
領域
databases
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。