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

Error on removal of composite keys on pgTable in postgres #170

Open
Odas0R opened this issue Aug 27, 2023 · 4 comments
Open

Error on removal of composite keys on pgTable in postgres #170

Odas0R opened this issue Aug 27, 2023 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@Odas0R
Copy link

Odas0R commented Aug 27, 2023

When removed a composite primary key and renamed a pgTable defined:

//old
const table = pgTable(
  "table",
  {
    id: text("id").notNull(),
    name: text("name").notNull(),
  },
  (t) => ({
    pk: primaryKey(t.id, t.name),
  }),
);

// new
const table = pgTable(
  "table_two",
  {
    id: text("id").notNull().primaryKey(),
    name: text("name").notNull(),
  }
);

Throws the error when npx drizzle-kit generate:pg:

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

Versions:

"drizzle-kit": "^0.19.13",
"drizzle-orm": "0.28.2",
@AndriiSherman AndriiSherman self-assigned this Aug 28, 2023
@AndriiSherman AndriiSherman added the bug Something isn't working label Aug 28, 2023
@dBianchii
Copy link

Happening to me a lot

@bestickley
Copy link

bestickley commented Apr 5, 2024

This is happening to me too when renaming a column which is a part of a composite primary key. After selecting "rename column" option here:

Is issue_id column in issue_upvote table created or renamed from another column?
❯ + issue_id               create column
  ~ feedback_id › issue_id rename column

I temporarily fixed it by changing node_modules/.pnpm/drizzle-kit@0.20.14/node_modules/drizzle-kit/bin.cjs:17115
from:
constraintName: json1.tables[tableName].compositePrimaryKeys[PgSquasher.unsquashPK(it).name].name
to
constraintName: json1.tables[tableName]?.compositePrimaryKeys[PgSquasher.unsquashPK(it).name].name

This issue did result in this line:
ALTER TABLE "lh"."issue_upvote" DROP CONSTRAINT "undefined";--> statement-breakpoint
so I fixed it by doing:
ALTER TABLE "lh"."issue_upvote" DROP CONSTRAINT "feedback_upvote_feedback_id_user_id_pk";--> statement-breakpoint

@mdingena
Copy link

I just ran into this same issue when choosing rename table option during generation. I followed @bestickley's steps and was able to resolve it.

@prichodko
Copy link

prichodko commented May 23, 2024

I saw this landed in v.0.21.3 - drizzle-team/drizzle-orm#2344.

@AndriiSherman do you know if the fix you implemented is applicable to this issue as well?

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

No branches or pull requests

6 participants