@@ -12,10 +12,10 @@ import {
1212 createRelationSchema ,
1313 extractVirtualProperty ,
1414 fixColumnAlias ,
15+ getMissingPrimaryKeyColumns ,
1516 getPaddedExpr ,
1617 getPropertiesByColumnName ,
1718 getQueryUrlComponents ,
18- includesAllPrimaryKeyColumns ,
1919 isDateColumnType ,
2020 isEntityKey ,
2121 isFindOperator ,
@@ -566,20 +566,26 @@ export async function paginate<T extends ObjectLiteral>(
566566
567567 // When we partial select the columns (main or relation) we must add the primary key column otherwise
568568 // typeorm will not be able to map the result.
569- let selectParams =
569+ // so, we check if the selected columns are a subset of the primary key columns
570+ // and add the missing ones.
571+ const selectParams =
570572 config . select && query . select && ! config . ignoreSelectInQueryParam
571573 ? config . select . filter ( ( column ) => query . select . includes ( column ) )
572574 : config . select
573- if ( ! includesAllPrimaryKeyColumns ( queryBuilder , query . select ) ) {
574- selectParams = config . select
575- }
576- if ( selectParams ?. length > 0 && includesAllPrimaryKeyColumns ( queryBuilder , selectParams ) ) {
577- const cols : string [ ] = selectParams . reduce ( ( cols , currentCol ) => {
575+
576+ if ( selectParams ?. length > 0 ) {
577+ let cols : string [ ] = selectParams . reduce ( ( cols , currentCol ) => {
578578 const columnProperties = getPropertiesByColumnName ( currentCol )
579579 const isRelation = checkIsRelation ( queryBuilder , columnProperties . propertyPath )
580580 cols . push ( fixColumnAlias ( columnProperties , queryBuilder . alias , isRelation ) )
581581 return cols
582582 } , [ ] )
583+
584+ const missingPrimaryKeys = getMissingPrimaryKeyColumns ( queryBuilder , cols )
585+ if ( missingPrimaryKeys . length > 0 ) {
586+ cols = cols . concat ( missingPrimaryKeys )
587+ }
588+
583589 queryBuilder . select ( cols )
584590 }
585591
0 commit comments