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

I am attempting to run db push, but I am getting this error above^^ does anyone know what might be going on?
33 Replies
Noahh
Noahh16mo ago
are you able to post the full stack trace and anywhere in your schema you're creating composite primary keys?
joshborseth
joshborsethOP16mo ago
absolutely! TypeError: Cannot read properties of undefined (reading 'compositePrimaryKeys') at /Users/joshuaborseth/Documents/locorum/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/index.cjs:16964:51 at Array.map (<anonymous>) at prepareDeleteCompositePrimaryKeyMySql (/Users/joshuaborseth/Documents/locorum/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/index.cjs:16959:33) at /Users/joshuaborseth/Documents/locorum/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/index.cjs:17352:35 at Array.forEach (<anonymous>) at applySnapshotsDiff (/Users/joshuaborseth/Documents/locorum/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/index.cjs:17277:26) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async prepareSQL (/Users/joshuaborseth/Documents/locorum/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/index.cjs:14879:14) at async prepareMySQLPush (/Users/joshuaborseth/Documents/locorum/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/index.cjs:14698:47) at async Command.<anonymous> (/Users/joshuaborseth/Documents/locorum/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/index.cjs:53223:22) I'll have a look for any composite primary keys... Im not using any, but someone on my team may be using them
Noahh
Noahh16mo ago
gotcha, it looks like this is a known issue: https://github.com/drizzle-team/drizzle-kit-mirror/issues/170
GitHub
Error on removal of composite keys on pgTable in postgres · Iss...
When removed a composite primary key and renamed a pgTable defined: //old const table = pgTable( "table", { id: text("id").notNull(), name: text("name").notNull(), }, ...
Noahh
Noahh16mo ago
maybe a primary key was removed from your schema recently?
joshborseth
joshborsethOP16mo ago
yeah I switched from using auto incrementing Ids to using uids so Im thinking that would do it
Noahh
Noahh16mo ago
interesting apparently reverting back, generating, then generating again helped someone? https://discord.com/channels/1043890932593987624/1070810929475883038/1145412231392149545
joshborseth
joshborsethOP16mo ago
hmm okay, ill give that a try got it working! thanks for your help @Noahh !!
Noahh
Noahh16mo ago
sweet
Rodrigo
Rodrigo10mo ago
For me this is happening when I'm simply renaming a table to uses a composite key of two other tables. Any idea how to resolve this? See https://discord.com/channels/1043890932593987624/1220342370957000724
Amruth Pillai
Amruth Pillai8mo ago
I'm getting this error when running drizzle-kit push on the latest version (0.21.0) as well, with the postgresql dialect. The schema has a lot of tables with composite primary keys, but most of them look like this:
export const workOrderTemplateUserTable = pgTable(
"work_order_template_user",
{
templateId: text("template_id")
.notNull()
.references(() => workOrderTemplateTable.id, { onDelete: "cascade" }),
userId: text("user_id")
.notNull()
.references(() => userTable.id, { onDelete: "cascade" }),
createdAt: timestampType("created_at"),
createdById: text("created_by_id").references(() => userTable.id, { onDelete: "set null" }),
},
(t) => ({
primaryKey: primaryKey({ columns: [t.templateId, t.userId] }),
}),
);
export const workOrderTemplateUserTable = pgTable(
"work_order_template_user",
{
templateId: text("template_id")
.notNull()
.references(() => workOrderTemplateTable.id, { onDelete: "cascade" }),
userId: text("user_id")
.notNull()
.references(() => userTable.id, { onDelete: "cascade" }),
createdAt: timestampType("created_at"),
createdById: text("created_by_id").references(() => userTable.id, { onDelete: "set null" }),
},
(t) => ({
primaryKey: primaryKey({ columns: [t.templateId, t.userId] }),
}),
);
This used to work as expected in the older versions, but it's become a problem now.
Tobi
Tobi8mo ago
yea same im getting issue as well. It used to work before updating
doogilie
doogilie8mo ago
same here
Ttttmizer
Ttttmizer8mo ago
having the same issue. I thought I was doing something wrong, but seeing all you having the same issue, I am guessing it's the docs or the tool that has issues? 🙂
DiamondDragon
DiamondDragon8mo ago
same @Andrew Sherman is this perhaps a more significant issue on the latest drizzle-kit version? seems like more people having issues. I tried to revert back to the last version and drizzle kit keeps wanting to drop columns with the pk in the comp pk even though there are no changes in the schema when running push. So I am not sure how to get past this using push at least, i keep getting Cannot read properties of undefined (reading 'compositePrimaryKeys')
Ttttmizer
Ttttmizer8mo ago
had it in the previous version too, so it's last two at least I think. I know it happens for me if I want to add certain kind of keys, so there is a workaround so far, and I also didn't try it directly with SQL instead of ORM methods
DiamondDragon
DiamondDragon8mo ago
Yeah I guess you can just write a sql file, kind of hard to keep track of if you're just iterating using push tho.. I haven't been able to get any of the solutions to work for me tho, so I am just stuck using single pk's on a few join tables
Ttttmizer
Ttttmizer8mo ago
same. hoping some future release will actually fix this 😄
Andrii Sherman
Andrii Sherman8mo ago
we are grouping all new kit issue and will do a patch release, I hope today/tomorrow we can release a new patch for it
adambarito
adambarito8mo ago
Same. Had no issues with drizzle-kit 0.20.17, and started seeing this compositePrimaryKeys error in 0.21.0 onwards.
Mika
Mika8mo ago
@Andrew Sherman new version (0.21.2) experiencing similar issue but with primaryKey
TypeError: Cannot read properties of undefined (reading 'primaryKey')
at logSuggestionsAndReturn2 (node_modules/drizzle-kit/bin.cjs:104764:87)
at sqlitePush (node_modules/drizzle-kit/bin.cjs:106845:19)
at async Command.<anonymous> (node_modules/drizzle-kit/bin.cjs:113749:7)
TypeError: Cannot read properties of undefined (reading 'primaryKey')
at logSuggestionsAndReturn2 (node_modules/drizzle-kit/bin.cjs:104764:87)
at sqlitePush (node_modules/drizzle-kit/bin.cjs:106845:19)
at async Command.<anonymous> (node_modules/drizzle-kit/bin.cjs:113749:7)
Was able to temporarily fix it by monkey patching the bin.cjs file referenced in the error, simply by adding optional chaining like so
No description
Mika
Mika8mo ago
You guys should probably consider adding "noUncheckedIndexedAccess": true, to Drizzle-kit tsconfig.json, otherwise these kinds of errors will always keep popping up
Aliy Akhbar
Aliy Akhbar8mo ago
are they fixing this issue? TypeError: Cannot read properties of undefined (reading 'primaryKey')
Andrii Sherman
Andrii Sherman8mo ago
yeah, I've included not all fixes on 0.21.2 working on fixing others
warflash
warflash8mo ago
Any workaround for the compositePimaryKeys issue perhaps? Not being able to generate migrations is a real pain 🙏
cebollo3
cebollo38mo ago
Hi there. I was facing the issue with TypeError: Cannot read properties of undefined (reading 'compositePrimaryKeys') In my case, I am using postgres and the 'public' schema. I was using drizzle-kit 0.21.2 I had to monkeypatch the applyPgSnapshotsDiff function in node_modules/drizzle-kit/bin.cjs to add a fallback to public when the schema is empty in the functions that were causing issues. Doing that, it no longer fails for me. My guess is that schema configuration should come from somewhere else but couldn't find it.
No description
Andrii Sherman
Andrii Sherman8mo ago
Should be fixed in 0.21.3 https://discord.com/channels/1043890932593987624/1235937884956000377/1242851388535607316 Would suggest to subscribe to kit notifications role
hk
hk8mo ago
I upgraded to 0.21.4 and still have the same error, when running push on an empty database it works for the first time but after that I keep getting Cannot read properties of undefined (reading 'compositePrimaryKeys') Here is my pgtable
export const table1 = pgTable(
'table1',
{
colm1: varchar('colm1', { length: 255 })
.notNull()
.references(() => ref1.id),
colm2: varchar('colm2', { length: 255 })
.notNull()
.references(() => ref2.id),
},
(table) => {
return {
pk: primaryKey({
columns: [table.colm1, table.colm2],
}),
};
},
);
export const table1 = pgTable(
'table1',
{
colm1: varchar('colm1', { length: 255 })
.notNull()
.references(() => ref1.id),
colm2: varchar('colm2', { length: 255 })
.notNull()
.references(() => ref2.id),
},
(table) => {
return {
pk: primaryKey({
columns: [table.colm1, table.colm2],
}),
};
},
);
Andrii Sherman
Andrii Sherman8mo ago
got it, thanks for a schema. fixing it
trlanzi
trlanzi8mo ago
I've been seeing the Cannot read properties of undefined (reading 'compositePrimaryKeys') as well. Upgraded to 0.21.3 and now whenever I db push, it tells me:
You're about to change table1 primary key. This statements may fail and you table may left without primary key
You're about to change table1 primary key. This statements may fail and you table may left without primary key
I have not made any changes to this table and even if I db push several times in a row, it always says the same thing. Upgrading to 0.21.4 didn't fix this. Here that table's schema:
export const table1 = pgTable(
"table1",
{
id: uuid("id")
.notNull()
.references(() => table2.id, { onDelete: "cascade" }),
},
(fs) => {
return {
pk: primaryKey({
name: "pk",
columns: [fs.id],
}),
};
},
);
export const table1 = pgTable(
"table1",
{
id: uuid("id")
.notNull()
.references(() => table2.id, { onDelete: "cascade" }),
},
(fs) => {
return {
pk: primaryKey({
name: "pk",
columns: [fs.id],
}),
};
},
);
Updating the schema to the following works, but I'd imagine both would be valid, no?
export const table1 = pgTable(
"table1",
{
id: uuid("id")
.notNull()
.references(() => table2.id, { onDelete: "cascade" })
.primaryKey(),
},
);
export const table1 = pgTable(
"table1",
{
id: uuid("id")
.notNull()
.references(() => table2.id, { onDelete: "cascade" })
.primaryKey(),
},
);
cebollo3
cebollo38mo ago
From 0.21.4 I can still reproduce the issue. Example schema:
export const testTable1 = pgTable("test_table", {
testId2: uuid("test_id2").primaryKey(),
});

export const testTable2 = pgTable("test_2", {
versionId: uuid("test_id2").primaryKey(),
});

export const bridgeTable = pgTable(
"bridge_table_some_name",
{
v1Id: uuid("bbbb_bbbbb_bbbb_test").references(
() => testTable2.versionId
),
v2Id: uuid("aaa_aaaaaa_aaaa_test").references(
() => testTable1.testId2
),
},
(table) => {
return {
pk: primaryKey({ columns: [table.v1Id, table.v2Id] }),
};
}
);
export const testTable1 = pgTable("test_table", {
testId2: uuid("test_id2").primaryKey(),
});

export const testTable2 = pgTable("test_2", {
versionId: uuid("test_id2").primaryKey(),
});

export const bridgeTable = pgTable(
"bridge_table_some_name",
{
v1Id: uuid("bbbb_bbbbb_bbbb_test").references(
() => testTable2.versionId
),
v2Id: uuid("aaa_aaaaaa_aaaa_test").references(
() => testTable1.testId2
),
},
(table) => {
return {
pk: primaryKey({ columns: [table.v1Id, table.v2Id] }),
};
}
);
Running the schema on an empty database works, running it a second time fails with the error
[✓] Pulling schema from database...TypeError: Cannot read properties of undefined (reading 'compositePrimaryKeys')
at /node_modules/drizzle-kit/bin.cjs:19393:67
at Array.map (<anonymous>)
at prepareDeleteCompositePrimaryKeyPg (/node_modules/drizzle-kit/bin.cjs:19387:33)
at applyPgSnapshotsDiff (/node_modules/drizzle-kit/bin.cjs:19890:33)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async preparePgPush /node_modules/drizzle-kit/bin.cjs:22139:52)
at async pgPush (/node_modules/drizzle-kit/bin.cjs:114468:26)
at async Command.<anonymous> (/node_modules/drizzle-kit/bin.cjs:121480:7)
[✓] Pulling schema from database...TypeError: Cannot read properties of undefined (reading 'compositePrimaryKeys')
at /node_modules/drizzle-kit/bin.cjs:19393:67
at Array.map (<anonymous>)
at prepareDeleteCompositePrimaryKeyPg (/node_modules/drizzle-kit/bin.cjs:19387:33)
at applyPgSnapshotsDiff (/node_modules/drizzle-kit/bin.cjs:19890:33)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async preparePgPush /node_modules/drizzle-kit/bin.cjs:22139:52)
at async pgPush (/node_modules/drizzle-kit/bin.cjs:114468:26)
at async Command.<anonymous> (/node_modules/drizzle-kit/bin.cjs:121480:7)
The names of the tables seem to matter. Switching, for example, the bridge table name to 'bridge_table' I can get Pulling schema from database...error: column "bbbb_bbbbb_bbbb_test" is in a primary key Hope this helps. All tables are in the 'public' schema.
hk
hk7mo ago
solution if the table name is too long then you need to set a primaryKey custom name
pk: primaryKey({
columns: [table.colm1, table.colm2],
name: 'custom_tabke_pkey',
})
pk: primaryKey({
columns: [table.colm1, table.colm2],
name: 'custom_tabke_pkey',
})
Furki4_4
Furki4_47mo ago
my column names are not too long, but still having the same issue. I can't even push the same migration after regenerating them from scratch. I tried giving custom names to composite keys but didn't work as well. kit version: 0.22.2 orm version: 0.31.1
Want results from more Discord servers?
Add your server