drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
24.71k stars 652 forks source link

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

Open rogiervandenberg opened 1 year ago

rogiervandenberg commented 1 year ago

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.:

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:

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.

Wargadji commented 8 months ago

Having the same issue, any updates on this? Using drizzle-kit 0.20.14

VincentSastra commented 8 months ago

Facing the same issue on 0.20.14, downgrade to 0.20.10 and it worked. Might be related to this issue: #636: drizzle-kit generates incorrect foreign key references

thisisbintang commented 7 months ago

still error Using drizzle-kit 0.20.10