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
22.17k stars 516 forks source link

[BUG]: Unique key reconciliation with upstream schema is inconsistent #2599

Open janyk-sucasa opened 3 weeks ago

janyk-sucasa commented 3 weeks ago

What version of drizzle-orm are you using?

0.31.2

What version of drizzle-kit are you using?

0.22.8

Describe the Bug

Unique keys aren't able to be reconciled properly using Drizzle with PlanetScale MySQL.

We have unique constraints defined using Drizzle Orm, either through the primary key constructor, or through specific unique key constructors i.e

id: serial('id').primaryKey(),

or (and see my comment above the code)

/*
    - this will always cause an error to throw when running yarn db:push against your feature db
    - your changes should get any other changes you've made should be applied anyway
    - think its an error with the drizzle push command not reconciling the constraint properly
    */
  email: varchar('email', {length: 255}).notNull().unique(),

We use DDL against feature branches in PlanetScale to create a schema change, then make a deploy request to PlanetScale.

A few things are happening:

  1. When we push unrelated schema changes to our DB using drizzle-kit push --config=drizzle.config.ts - we get an error complaining the above constraint already exists i.e You're about to add users_email_unique unique constraint
  2. When we make deploy requests after adding unrelated tables/column changes, we'll find that Unique Keys from random tables are being dropped Screenshot 2024-07-08 at 12 13 10 PM

Expected behavior

  1. Unique keys are able to be reconciled properly through introspection
  2. Unique keys wouldn't randomly be dropped through DDL commands against our planetscale feature branches, through unrelated changes

Environment & setup

Node JS: 20.14.0

AndriiSherman commented 2 weeks ago

thanks, taking that into work