drizzle-team / drizzle-team/drizzle-orm

[BUG]: Drizzle Kit introspection creates different foreign key names than migration

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

Description

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

0.29.0

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

0.20.4

### Describe the Bug

When introspecting my current (built with Prisma) database with `npx drizzle-kit introspect:pg` I get a schema.ts file and .sql file with the migrations. The foreign keys between tables are named following my current database.

However, when I run `npx drizzle-kit generate:pg` after that, all my tables are altered and all constraints are being removed, in order to be added again with a new name, e.g.:

```SQL
ALTER TABLE "permissions_spaces" DROP CONSTRAINT "permissions_spaces_user_id_fkey";
...
DO $$ BEGIN
ALTER TABLE "permissions_spaces" ADD CONSTRAINT "permissions_spaces_user_id_auth_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "auth_users"("id") ON DELETE cascade ON UPDATE cascade;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
```

### Expected behavior

I would expect the generated `schema.ts` file to be having named Foreign Keys, according to my database, something like:

```typescript
userReference: foreignKey({
columns: [table.userId],
foreignColumns: [authUsers.id],
name: 'permissions_spaces_user_id_fkey', // <--- this is the name of the foreign key
}),
```

..so my schema.ts file is actually reflecting my DB.

### Environment & setup

Mac, running Drizzle Kit on node v20.9.0 in my terminal to introspect PostgreSQL and to generate new migrations.

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with `npx drizzle-kit introspect:pg` followed by `npx drizzle-kit generate:pg`, then compare the generated `schema.ts` and migration `.sql` files with the existing PostgreSQL foreign-key names. Done means introspection preserves names such as `permissions_spaces_user_id_fkey` and generation no longer drops and recreates unchanged constraints under new names.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, postgresql, typescript
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.