Skip to content

[BUG]:drizzle-kit pull generates incorrect schema for functional/computed index #5224

@daveset-mytownai

Description

@daveset-mytownai

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

What version of drizzle-kit are you using?

1.0.0-beta.8

Other packages

No response

Describe the Bug

Postgresql 15.x

I have an addresses table with standard city, county, state, zip and a normalized_address column.

We have a unique index that forces to upper case for supporting case-insensitive search:

  CREATE UNIQUE INDEX "idx_addresses_natural_key" ON "s1"."addresses" (
    upper(normalized_address),
    upper(COALESCE(city, '')::text),
    upper(COALESCE(county, '')::text),
    upper(state::text),
    upper(COALESCE(zip, '')::text)
  ) WHERE ((normalized_address IS NOT NULL) AND (state IS NOT NULL));

drizzle-kit 1.0.0-beta.8 is introspecting this and generating the following index clause on the table

    uniqueIndex("idx_addresses_natural_key")
      .using("btree", sql.raw("true"), sql.raw("true"), sql.raw("true"), sql.raw("true"), sql.raw("true"))
      .where(sql`((normalized_address IS NOT NULL) AND (state IS NOT NULL))`),

in schema.ts, which becomes

CREATE UNIQUE INDEX "idx_addresses_natural_key" ON "s1"."addresses" (true,true,true,true,true) WHERE ((normalized_address IS NOT NULL) AND (state IS NOT NULL));--> statement-breakpoint

in a migration.

This is incorrect and will break the index if applied.

I am able to workaround by manually updating schema.ts to match what is in migration.sql, and this is the expected output (or something similar that produces a correct index and doesn't result in a migration):

    uniqueIndex("idx_addresses_natural_key")
      .using(
        "btree",
        sql.raw("upper(normalized_address)"),
        sql.raw(`upper((COALESCE(city, ''::character varying))::text)`),
        sql.raw(`upper((COALESCE(county, ''::character varying))::text)`),
        sql.raw(`upper((state)::text)`),
        sql.raw(`upper((COALESCE(zip, ''::character varying))::text)`),
      )
      .where(sql.raw(`((normalized_address IS NOT NULL) AND (state IS NOT NULL))`)),

FWIW a slightly simpler example CREATE INDEX "idx_address_upper_normalized" ON "s1"."addresses" (upper(normalized_address) has the same problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions