drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: FK Migration Enforcement

Open
#4,458 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Feature hasn't been suggested before.

- [x] I have verified this feature I'm about to request hasn't been suggested before.

### Describe the enhancement you want to request

Recommend a feature to ensure this happens automatically and safely... you guys already added nice CLI options for correcting table name changes... just need the same for FKs and index changes. Kinda dangerous to generate migrations without it and/or without warning 'migration could not be completely accurately generated because..."

AI Explanation
Drizzle’s migration generator is essentially a “diff” tool that spits out the minimal SQL needed to make your individual table definitions match the new schema. It doesn’t perform global dependency analysis — it won’t:

Inspect other tables’ foreign-key constraints

Reorder DROP / CREATE statements to satisfy those cross-table dependencies

Automatically drop the FK before dropping its backing unique index

In practice, that means if you rename or drop an index that some other table’s FK points at, Drizzle will happily generate the ALTER TABLE … DROP INDEX … for you, but it won’t notice that there’s a live foreign key relying on it — that check only happens when MySQL actually runs the SQL.

If you need Drizzle to generate a fully “safe” migration in this situation, you’ll have to either:

Manually edit the generated SQL to drop the FK first (as shown earlier), then drop the index, then (optionally) recreate both.

Or split your change into two migrations:

Migration A: drop the FK constraint on the child table, then drop/rename the index on the parent.

Migration B: re-add the FK against the new index.

It’s a known limitation of most schema-diff tools — they work “table by table,” so cross-table dependency order still needs human supervision.

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.