Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vladinator1000 opened this issue Feb 15, 2025 · 1 comment
Labels
d1 Relating to D1

Comments

@vladinator1000
Copy link

vladinator1000 commented Feb 15, 2025

I have this Drizzle schema

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

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:

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?

@lambrospetrou
Copy link
Contributor

I believe this is what we get from SQLite:

sqlite> select * from users;
lambros|petrou
sqlite> select * from accounts;
acc_1|testAcc|lambros
sqlite> delete from users where id = 'lambros';
Runtime error: FOREIGN KEY constraint failed (19)

This will probably need internal changes, so it needs full investigation if and how to be done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
d1 Relating to D1
Projects
Status: Untriaged
Development

No branches or pull requests

3 participants