drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

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

Open ghost opened this issue 1 year ago • 52 comments

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

ghost avatar May 17 '24 20:05 ghost

This should be fixed in [email protected]. If the issue persists, feel free to reopen it!

AndriiSherman avatar May 22 '24 14:05 AndriiSherman

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.

sakulstra avatar May 28 '24 15:05 sakulstra

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/[email protected]/node_modules/drizzle-kit/bin.cjs:19393:67
    at Array.map (<anonymous>)
    at prepareDeleteCompositePrimaryKeyPg (/Users/brandon/brevian/platform/control-plane/app/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:19387:33)
    at applyPgSnapshotsDiff (/app/node_modules/.pnpm/[email protected]/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/[email protected]/node_modules/drizzle-kit/bin.cjs:22139:52)
    at async pgPush (/app/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:114468:26)
    at async Command.<anonymous> (/app/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:121480:7)

BrandonWeng avatar May 28 '24 16:05 BrandonWeng

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

lhorak avatar May 30 '24 12:05 lhorak

I solved this issue by modifying the package source for [email protected], 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
         };
       });
     };

LavaToaster avatar Jun 01 '24 15:06 LavaToaster

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?

goldoraf avatar Jun 06 '24 07:06 goldoraf

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

LavaToaster avatar Jun 06 '24 07:06 LavaToaster

why is this marked as closed? can you reopen this? 🙏

AlonMiz avatar Jun 10 '24 12:06 AlonMiz

+1 to reopening this. Using drizzle-kit generate and then drizzle-kit migrate works, but not drizzle-kit push (I get the error described here).

dkmooers avatar Jun 10 '24 20:06 dkmooers

Experiencing this issue as well when attempting to rename a table during both drizzle-kit generate and drizzle-kit push

waltershewmake avatar Jun 19 '24 00:06 waltershewmake

Same issue on my end

juanamd avatar Jun 19 '24 19:06 juanamd

Yep getting this issue as well. Whatever schema change I make, when I run push I just get that error

FuddingtonBear avatar Jun 21 '24 09:06 FuddingtonBear

Yeah same issue here

NickWoodward avatar Jun 23 '24 00:06 NickWoodward

@AndriiSherman, could you please reopen this issue? It is still present. @LavaToaster's solution is working, but it would be great if the patch landed in drizzle-kit.

Thank you!

nacho-vazquez avatar Jun 25 '24 21:06 nacho-vazquez

The issue still persists in [email protected]

migrate doesn't work either.

tgdn avatar Jun 28 '24 12:06 tgdn

Yes, the issue still persists. This error is triggered once I run the generate command.

I get this error once I rename a table name (snake case) with a composite prime key.

Versions: "drizzle-orm": "^0.31.2" "drizzle-kit": "^0.22.7"

Please re-open it

cawak avatar Jun 28 '24 14:06 cawak

I am on latest versions

"drizzle-orm": "^0.31.2" "drizzle-kit": "^0.22.7"

Drizzle is not usable without solving this issue and it's closed!!!! it's almost 2 months :/

Thanks for @LavaToaster I can continue working!

Please reopen the issue...

mustafamoe avatar Jun 29 '24 11:06 mustafamoe

I am on latest versions

"drizzle-orm": "^0.31.2" "drizzle-kit": "^0.22.7"

Drizzle is not usable without solving this issue and it's closed!!!! it's almost 2 months. maybe it was a mistake using drizzle this early. what a joke :/

Thanks for @LavaToaster I can continue working!

Please reopen the issue...

Just want to point out that this is open source software and there are real good people behind making it, putting in a ton of work, benefiting a lot of people

dkmooers avatar Jun 29 '24 11:06 dkmooers

I am on latest versions "drizzle-orm": "^0.31.2" "drizzle-kit": "^0.22.7" Drizzle is not usable without solving this issue and it's closed!!!! it's almost 2 months. maybe it was a mistake using drizzle this early. what a joke :/ Thanks for @LavaToaster I can continue working! Please reopen the issue...

Just want to point out that this is open source software and there are real good people behind making it, putting in a ton of work, benefiting a lot of people

Still people are getting paid working on it, correct me if am wrong.

mustafamoe avatar Jun 29 '24 11:06 mustafamoe

I am on latest versions "drizzle-orm": "^0.31.2" "drizzle-kit": "^0.22.7" Drizzle is not usable without solving this issue and it's closed!!!! it's almost 2 months. maybe it was a mistake using drizzle this early. what a joke :/ Thanks for @LavaToaster I can continue working! Please reopen the issue...

Just want to point out that this is open source software and there are real good people behind making it, putting in a ton of work, benefiting a lot of people

Still people are getting paid working on it, correct me if am wrong.

I can't speak to that, but I can say that even if I was getting paid to work on Drizzle, a comment like "what a joke" would be pretty off-putting and not feel that good to hear. I'm all for keeping this a supportive environment

dkmooers avatar Jun 29 '24 11:06 dkmooers

I am on latest versions "drizzle-orm": "^0.31.2" "drizzle-kit": "^0.22.7" Drizzle is not usable without solving this issue and it's closed!!!! it's almost 2 months. maybe it was a mistake using drizzle this early. what a joke :/ Thanks for @LavaToaster I can continue working! Please reopen the issue...

Just want to point out that this is open source software and there are real good people behind making it, putting in a ton of work, benefiting a lot of people

Still people are getting paid working on it, correct me if am wrong.

I can't speak to that, but I can say that even if I was getting paid to work on Drizzle, a comment like "what a joke" would be pretty off-putting and not feel that good to hear. I'm all for keeping this a supportive environment

Sorry if anybody took it offensively, but almost 2 months for 1 line fix kinda funny no 😅

mustafamoe avatar Jun 29 '24 11:06 mustafamoe

Hey everyone, sorry for the late response. I was really busy with other features we were working on. Let me push the patch update now so you can be unblocked

AndriiSherman avatar Jun 29 '24 11:06 AndriiSherman

@LavaToaster thanks a lot!

AndriiSherman avatar Jun 29 '24 11:06 AndriiSherman

Ok, yeah, it was my bad for not adding public by default. I'm publishing a patch update and will mention @LavaToaster as the one who fixed it

AndriiSherman avatar Jun 29 '24 11:06 AndriiSherman

please check [email protected], should have the fix for that

AndriiSherman avatar Jun 29 '24 12:06 AndriiSherman

Thank you @AndriiSherman you are appreciated

NachoVazquez avatar Jun 29 '24 12:06 NachoVazquez

yup it's working now, thanks alot @LavaToaster @AndriiSherman!

mustafamoe avatar Jun 29 '24 12:06 mustafamoe

Just want to point out that this is open source software and there are real good people behind making it, putting in a ton of work, benefiting a lot of people

Drizzle Kit is closed source

jonahsnider avatar Jun 30 '24 10:06 jonahsnider

Still experiencing this bug after upgrading to [email protected]. In my case, when trying to rename a table with a camelCased name. The table does not contain any composite primary keys though.

juanamd avatar Jul 01 '24 19:07 juanamd

Still experiencing the same bug even after the upgrade . TypeError: Cannot read properties of undefined (reading 'compositePrimaryKeys') at ***/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:27627:79 at Array.map (<anonymous>) at prepareDeleteCompositePrimaryKeyPg (***/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:27621:33) at applyPgSnapshotsDiff (***/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:28130:33) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async preparePgPush (***/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:30406:52) at async pgPush (***/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:122504:26) at async _Command.<anonymous> (***/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:129681:7)

LargatSeif avatar Jul 08 '24 22:07 LargatSeif