Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix drizzle-kit push crash on pulling db schema with compound indexes that have expression members #3888

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jaredp
Copy link

@jaredp jaredp commented Jan 2, 2025

fix #3745
See my comment #3745 (comment)

drizzle-kit push crashes with zod error Expected string, received null on tables like

// approx translation of original bug reporter's example
export const translations = pgTable(
  "translation",
  {
    source_language: text("source_language"),
    source_text: text("source_text"),
    target_language: text("target_language"),
    target_text: text("target_text"),
  },
  (table) => ({
    tnsltn: index("tnsltn").on(
      table.source_language,
      sql<string>`(md5((${table.source_text})::text))`,
    ),
  }),
);

The crash happens when there's a compound index on (col, expression(col)) where one of the index components is expression(col), not just a column.

It happens when drizzle-kit push ingests the db's current schema. This is extra confusing because drizzle-kit push succeeds when adding the index to a db that doesn't have it yet. It's only in subsequent syncs that drizzle-kit crashes trying to read back the index it created.

The null causes zod to error out when trying to parse the schema pulled from postgres. The schema it's handed has the inappropriate null constructed in fromDatabase, where:

The dbIndexes it pulls from postgres has a column_name=NULL for the row with indexname='tnsltn', index_order=2, where it ought to have column_name='md5((source_text)::text)'.

I confirmed this in my project, which has a table with similar index to that of the original bug reporter. I worked with Claude to generate this new SQL query. I confirmed that this changed SQL query produced by Claude works for my project.

However, I'm personally not great at SQL, so do very much take this with a grain of salt. I can't meaningfully read the original query, nor the one Claude wrote.

Hopefully someone with better SQL can read and give feedback!

@jaredp jaredp changed the title fix drizzle-kit push on schemas with compound indexes with expression members fix drizzle-kit push crash on pulling db schema with compound indexes that have expression members Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG]: drizzle-kit pull doesn't work with md5 index
1 participant