Skip to content

[BUG]: [Drizzle-kit][Pg] Altering a UNIQUE constraint: generated SQL ignores the new changes #5585

@CedricDsmet

Description

@CedricDsmet

Report hasn't been filed before.

  • I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

1.0.0-beta.20

What version of drizzle-kit are you using?

1.0.0-beta.20

Other packages

No response

Describe the Bug

  • drizzle-kit
  • postgres dialect

When modifying an existing unique constraint, drizzle-kit generate correctly detects the change and reports it in the migration summary.

However, the generated SQL is incorrect: the ADD CONSTRAINT statement is missing the the changes that were made. The migration will silently re-create the old constraint without applying the intended change.

In my specific case i tried adding .nullsNotDistinct() to an existing unique constraint. Below you see the output of the drizzle-kit generate command.

┌─── public.attrition_timeline.attrition_timeline_unique unique changed:
│ nullsNotDistinct: false -> true
├───
│ ALTER TABLE "attrition_timeline" DROP CONSTRAINT "attrition_timeline_unique";
│ ALTER TABLE "attrition_timeline" ADD CONSTRAINT "attrition_timeline_unique" UNIQUE("job_holder_aggregation_id","scenario_id","year");
└───

As you can see the change what detected, but the generated SQL did not add NULLS NOT DISTINCT

Generated (incorrect):

ALTER TABLE "attrition_timeline" DROP CONSTRAINT "attrition_timeline_unique";
ALTER TABLE "attrition_timeline" ADD CONSTRAINT "attrition_timeline_unique" UNIQUE("job_holder_aggregation_id","scenario_id","year");

Expected:

ALTER TABLE "attrition_timeline" DROP CONSTRAINT "attrition_timeline_unique";
ALTER TABLE "attrition_timeline" ADD CONSTRAINT "attrition_timeline_unique" UNIQUE NULLS NOT DISTINCT ("job_holder_aggregation_id","scenario_id","year");

After looking at the code and doing some further testing it seems that any change to a unique constaint is not being picked up. Adding or removing a column for example is also not reflected in the generated SQL.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions