drizzle-team / drizzle-team/drizzle-orm

[BUG]: Cloudflare D1 FOREIGN KEY constraint failed

Open
#4,089 11 comments 14 reactions 0 assignees View on GitHub
bug db/sqlite drizzle/kit priority
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Report hasn't been filed before.

- [x] I have verified that the bug I'm about to report hasn't been filed before.

### What version of `drizzle-orm` are you using?

0.38.3

### What version of `drizzle-kit` are you using?

0.30.1

### Other packages

Astro js, Cloudflare Pages

### Describe the Bug

### Issue: Foreign Key Constraint Handling in Cloudflare D1 Migrations

When generating migrations for Cloudflare D1, Drizzle-ORM uses the statement:

`PRAGMA foreign_keys = OFF; --> statement-breakpoint`

However, this causes migrations to fail when executed. According to Cloudflare's [documentation](https://developers.cloudflare.com/d1/best-practices/import-export-data/) (at the bottom), the right way is

`PRAGMA defer_foreign_keys = true; --> statement-breakpoint`

Got Confused about whether the issue was Cloudflare or Drizzle still am. If tables have data and we generate new migrations they fail to execute throwing `X [ERROR] FOREIGN KEY constraint failed: SQLITE_CONSTRAINT` Updating the statement manually with the one specified in the CF docs fixes the issue.

It would be nice to get a warning or some hint that this might happen. Checked Drizzle docs, but nothing specific. Would appreciate a heads-up while generating migrations if possible.

Thanks for the awesome tool.

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.