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

[BUG]: Unable to generate PG migration when renaming table with composite PK

Open L-Mario564 opened this issue 2 years ago • 2 comments

What version of drizzle-orm are you using?

0.29.3

What version of drizzle-kit are you using?

0.20.9

Describe the Bug

I have the following table in my schema:

export const UserAnsweredQuestions = pgTable('user_answered_questions', {
  ...
}, (table) => ({
  pk: primaryKey({
    columns: [table.userId, table.surveyQuestionId]
  })
}));

And I wanted to rename it to be singular, instead of plural:

export const UserAnsweredQuestion = pgTable('user_answered_question', { ... }));

Upon executing npx drizzle-kit generate:pg and accepting the rename option, I get this error:

No config path provided, using default 'drizzle.config.js'
Reading config file 'C:\Users\...\project\drizzle.config.js'
~ user_answered_questions › user_answered_question table will be renamed
--- all table conflicts resolved ---

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

Expected behavior

This should not error. It should rename the table without issues.

Environment & setup

OS: Windows 11 Node.js: v18.18.0

L-Mario564 avatar Jan 04 '24 21:01 L-Mario564

I can confirm the same error when doing a drizzle-kit push:pg

after renaming table committeeMembership › member.

❯ pn run drizzle:push

> [email protected] drizzle:push /Users/nils/code/prisma
> drizzle-kit push:pg --config=drizzle.config.ts

drizzle-kit: v0.20.13
drizzle-orm: v0.29.3

Custom config path was provided, using 'drizzle.config.ts'
Reading config file '/Users/nils/code/prisma/drizzle.config.ts'
~ committeeMembership › member table will be renamed
--- all table conflicts resolved ---

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

I renamed manually and failed with a referenceTable error form drizzle/orm in the app.

⨯ node_modules/drizzle-orm/relations.js (212:30) @ Module.normalizeRelation ⨯ TypeError: Cannot read properties of undefined (reading 'referencedTable')

ALTER TABLE committeeMembership
RENAME TO member;
-- drizzle push:pg -> successful
-- pn run dev -> fails with error above
ALTER TABLE member
RENAME TO committeeMembership;
-- all good again

neslob avatar Jan 18 '24 22:01 neslob

Hi all,

This seems like a real problem, i just upgraded to 0.21.0 now every time i make a schema change i get this compositePrimaryKey undefined error and have to completely wipe my DB and start fresh to push anything to neon.

Even a simple change like adding a new column to any table causes me to get this error.

Its making drizzle nearly unusable

ITSJORDAN-TFA avatar May 11 '24 03:05 ITSJORDAN-TFA

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

AndriiSherman avatar Jun 29 '24 12:06 AndriiSherman