Drizzle: undefined (reading 'compositePrimaryKeys') error
Programming
3/10/2024
2 minutes
Author: Dawid

Available languages:

Drizzle: undefined (reading 'compositePrimaryKeys') error

So you started your Project with Drizzle, and then you wanted to change the name of table with drizzle-kit, but instead of successfully updating your table name, you got TypeError: Cannot read properties of undefined (reading 'compositePrimaryKeys')? Let me explain to you why this Error occurring with potential fix.

As you probably know (or not), Drizzle-kit is in beta version and this particular problem is very well-known in Drizzle-kit, but unfortunately is not fixed since 2023. This TypeError is occuring when you have a table with FK (Foreign keys), so for expample we have a Table named session:

export const sessions = createTable(
"session",
{
sessionToken: varchar("sessionToken", { length: 255 })
.notNull()
.primaryKey(),
userId: varchar("userId", { length: 255 }).notNull(),
expires: timestamp("expires", { mode: "date" }).notNull(),
},
(session) => ({
userIdIdx: index("session_userId_idx").on(session.userId),
}),
);

As you can see we have connection between session table and user table. Now if u want to rename session table to for example => Sessions then you will get a 'compositePrimaryKeys' TypeError which lead to data loss (dropping whole table). You don't want to do this. I came across with solution which work pretty well. So if you are getting this error and want to for example rename the table then you need to do it manually through MySQL (I'm using MySQL as db, but it will work in any other languages). If you are hosting your Database to external providers like PlanetScale, then you can directly execute MySQL scripts though UI (if you host locally you can use PHP Admin or something else). The scripts are well-known, so it should not be so hard to do it, but you need to be careful, because you can break your Database, that's why I recommend doing a Database backup before doing it. In my use case, I wanted to rename my table, so I execute: RENAME TABLE old_table TO new_table

After that, the table was successfully renamed, and I didn't break anything in Database and Drizzle Schema.



As I already said this bug in Drizzle-kit is known in Community and you can track the progress in this GitHub thread: https://github.com/drizzle-team/drizzle-orm/issues/1144 I hope I could help you with that. If you have any questions, please free to comment or contact me.

About the author

avatar

Penify creator, Fullstack developer, Typescript, Web, Apps, Blogger

I am an experienced Fullstack web developer and the creator of Penify blog platform. I am excited and motivated to explore new technologies. I am a dedicated and hardworking person with a will to create the best user experience.

compositePrimaryKeys
Drizzle
Drizzle-kit
fix
MySQL
typeError

Comments

Be the first to comment on this article!