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

[BUG]: TypeError: Cannot read properties of undefined (reading 'compositePrimaryKeys') #2344

Closed
terlan4 opened this issue May 17, 2024 · 9 comments
Labels
bug Something isn't working drizzle-kit

Comments

@terlan4
Copy link

terlan4 commented May 17, 2024

What version of drizzle-orm are you using?

0.30.10

What version of drizzle-kit are you using?

0.21.2

Describe the Bug

if I have tables with no composite primary keys(table => pk: primaryKey(....) , then i can change my column names and do whatever i want with no issue in push.

issues arises when:

  • changing column names, adding new columns, tables to schema that any table has composite primary key.
  • if schema already exists in database and i want to add composite primary key.

everytime before pushing new version of the schema, i have to drop all my tables in db. otherwise drizzle-kit push command keeps failing with this error.

image

Expected behavior

No response

Environment & setup

next.js 14 app router application

@terlan4 terlan4 added the bug Something isn't working label May 17, 2024
@AndriiSherman
Copy link
Member

This should be fixed in drizzle-kit@0.21.3. If the issue persists, feel free to reopen it!

@terlan4
Copy link
Author

terlan4 commented May 26, 2024

image

Issue is still there after updating. tried with both 0.21.3 and 0.21.4
@AndriiSherman

@sakulstra
Copy link

I can confirm the issue still exists even on 22 beta.

As a variant of the problem, the same issue exists when renaming a table, not only a field.

@BrandonWeng
Copy link

Also having the same issues

drizzle-kit: v0.21.4
drizzle-orm: v0.30.10

No config path provided, using default path

Using 'pg' driver for database querying
[✓] Pulling schema from database...TypeError: Cannot read properties of undefined (reading 'compositePrimaryKeys')
    at /app/node_modules/.pnpm/drizzle-kit@0.21.4/node_modules/drizzle-kit/bin.cjs:19393:67
    at Array.map (<anonymous>)
    at prepareDeleteCompositePrimaryKeyPg (/Users/brandon/brevian/platform/control-plane/app/node_modules/.pnpm/drizzle-kit@0.21.4/node_modules/drizzle-kit/bin.cjs:19387:33)
    at applyPgSnapshotsDiff (/app/node_modules/.pnpm/drizzle-kit@0.21.4/node_modules/drizzle-kit/bin.cjs:19890:33)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async preparePgPush /app/node_modules/.pnpm/drizzle-kit@0.21.4/node_modules/drizzle-kit/bin.cjs:22139:52)
    at async pgPush (/app/node_modules/.pnpm/drizzle-kit@0.21.4/node_modules/drizzle-kit/bin.cjs:114468:26)
    at async Command.<anonymous> (/app/node_modules/.pnpm/drizzle-kit@0.21.4/node_modules/drizzle-kit/bin.cjs:121480:7)

@terlan4
Copy link
Author

terlan4 commented May 29, 2024

@sakulstra yes, the issue happens with any type of schema change. Just gave this one as a sample since it is very minor change. but it fails no matter what you do

@lhorak
Copy link

lhorak commented May 30, 2024

I ran into the same issue, and I suspect this is related to naming conventions (camelCase vs snake case). As an example:

export const table = pgTable(
  "tableName",
  {
    timestamp: timestamp("timestamp").notNull().defaultNow(),
    userName: text("ws_id").notNull(),
  },
  (tb) => ({
    pk: primaryKey({ columns: [tb.userName, tb.timestamp] }),
  })
);

this will correctly create the userName_timestamp_pk, however, when additional changes occur and you're trying to push, drizzle-kit is trying to find username_timestamp_pk which cannot be found, thus throwing the undefined error.

I converted my table names to snake case and it seems like this solved the issue

@LavaToaster
Copy link

I solved this issue by modifying the package source for drizzle-kit@0.22.1, now have a manual patch for drizzle kit.

diff --git a/bin.cjs b/bin.cjs
index e5e7558dc577c4fba1ca4fe508a5851b4e8f4c91..e42056bb4937c68c0598be71ea8b63cea437dccc 100755
--- a/bin.cjs
+++ b/bin.cjs
@@ -27620,7 +27620,7 @@ var init_jsonStatements = __esm({
           tableName,
           data: it,
           schema: schema5,
-          constraintName: json1.tables[`${schema5}.${tableName}`].compositePrimaryKeys[PgSquasher.unsquashPK(it).name].name
+          constraintName: json1.tables[`${schema5 || "public"}.${tableName}`].compositePrimaryKeys[PgSquasher.unsquashPK(it).name].name
         };
       });
     };

@goldoraf
Copy link

goldoraf commented Jun 6, 2024

I can confirm the issue is still there and that the above fix from @LavaToaster solves the problem. @LavaToaster would you like to submit a PR? @AndriiSherman could you please reopen the issue?

@LavaToaster
Copy link

@goldoraf drizzle kit is not open source yet. So, cannot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working drizzle-kit
Projects
None yet
Development

No branches or pull requests

8 participants