@@ -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 ) ;
0 commit comments