Skip to content

Commit 7a995a7

Browse files
committed
fix: retain composite PK columns
1 parent 4877ac3 commit 7a995a7

2 files changed

Lines changed: 30 additions & 26 deletions

File tree

src/tables.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,28 @@ const createZeroTableBuilder = <
264264
const tableColumns = getTableColumns(table);
265265
const tableConfig = getTableConfigForDatabase(table);
266266

267-
const primaryKeysFromColumns: string[] = [];
267+
const columnNameToStableKey = new Map<string, string>(
268+
typedEntries(tableColumns).map(([key, column]) => [
269+
column.name,
270+
String(key),
271+
]),
272+
);
273+
274+
const primaryKeys = new Set<string>();
275+
for (const [key, column] of typedEntries(tableColumns)) {
276+
if (column.primary) {
277+
primaryKeys.add(String(key));
278+
}
279+
}
280+
281+
for (const pk of tableConfig.primaryKeys) {
282+
for (const pkColumn of pk.columns) {
283+
const key = columnNameToStableKey.get(pkColumn.name);
284+
if (key) {
285+
primaryKeys.add(String(key));
286+
}
287+
}
288+
}
268289

269290
const isColumnBuilder = (value: unknown): value is ColumnBuilder<any> =>
270291
typeof value === "object" && value !== null && "schema" in value;
@@ -299,7 +320,7 @@ const createZeroTableBuilder = <
299320
if (
300321
columnConfig !== true &&
301322
!isColumnConfigOverride &&
302-
!column.primary
323+
!primaryKeys.has(String(key))
303324
) {
304325
debugLog(
305326
debug,
@@ -335,10 +356,6 @@ const createZeroTableBuilder = <
335356
? columnConfig.schema.optional
336357
: false;
337358

338-
if (column.primary) {
339-
primaryKeysFromColumns.push(String(key));
340-
}
341-
342359
if (columnConfig && typeof columnConfig !== "boolean") {
343360
return {
344361
...acc,
@@ -371,19 +388,7 @@ const createZeroTableBuilder = <
371388
{} as Record<string, any>,
372389
);
373390

374-
const primaryKeys = [
375-
...primaryKeysFromColumns,
376-
...tableConfig.primaryKeys.flatMap((k) =>
377-
k.columns.map((c) =>
378-
getDrizzleColumnKeyFromColumnName({
379-
columnName: c.name,
380-
table: c.table,
381-
}),
382-
),
383-
),
384-
];
385-
386-
if (!primaryKeys.length) {
391+
if (primaryKeys.size === 0) {
387392
throw new Error(
388393
`drizzle-zero: No primary keys found in table - ${actualTableName}. Did you forget to define a primary key?`,
389394
);

tests/config.test.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,11 @@ describe("drizzleZeroConfig with explicit table and column configuration", () =>
6464
test("should handle explicit table and column configurations", () => {
6565
const schema = drizzleZeroConfig(drizzleSchema, {
6666
tables: {
67-
users: {
68-
name: true,
69-
email: false,
70-
// phone is not mentioned, should be excluded
67+
users: true, // include all columns
68+
usersToPosts: {
69+
userId: false, // will be included anyway because it is part of the primary key
70+
// role is not mentioned, should be excluded
7171
},
72-
usersToPosts: true, // include all columns
7372
posts: false,
7473
// comments table is not mentioned, should be excluded
7574
},
@@ -88,11 +87,11 @@ describe("drizzleZeroConfig with explicit table and column configuration", () =>
8887
// `users` table should have `id` (pk) and `name`
8988
expect(
9089
new Set(Object.keys((schema.tables as any).users.columns)),
91-
).toStrictEqual(new Set(["id", "name"]));
90+
).toStrictEqual(new Set(["id", "name", "email", "phone"]));
9291

9392
// `usersToPosts` table should have all its columns
9493
expect(
9594
new Set(Object.keys((schema.tables as any).usersToPosts.columns)),
96-
).toStrictEqual(new Set(["userId", "postId", "role"]));
95+
).toStrictEqual(new Set(["userId", "postId"]));
9796
});
9897
});

0 commit comments

Comments
 (0)