fix: add _rel suffix to relationship columns to avoid duplicate columns (#649)

This commit is contained in:
Robin De Schepper 2023-07-16 19:43:36 +02:00 committed by GitHub
parent 34dff5ef09
commit d328e343a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 7 deletions

View File

@ -137,11 +137,16 @@ export function fixColumnAlias(
): string { ): string {
if (isRelation) { if (isRelation) {
if (isVirtualProperty && query) { if (isVirtualProperty && query) {
return `(${query(`${alias}_${properties.propertyPath}`)})` // () is needed to avoid parameter conflict return `(${query(`${alias}_${properties.propertyPath}_rel`)})` // () is needed to avoid parameter conflict
} else if ((isVirtualProperty && !query) || properties.isNested) { } else if ((isVirtualProperty && !query) || properties.isNested) {
return `${alias}_${properties.propertyPath}_${properties.propertyName}` if (properties.propertyName.includes('.')) {
const [nestedRel, nestedCol] = properties.propertyName.split('.')
return `${alias}_${properties.propertyPath}_rel_${nestedRel}_rel.${nestedCol}`
} else { } else {
return `${alias}_${properties.propertyPath}.${properties.propertyName}` return `${alias}_${properties.propertyPath}_rel_${properties.propertyName}`
}
} else {
return `${alias}_${properties.propertyPath}_rel.${properties.propertyName}`
} }
} else if (isVirtualProperty) { } else if (isVirtualProperty) {
return query ? `(${query(`${alias}`)})` : `${alias}_${properties.propertyName}` return query ? `(${query(`${alias}`)})` : `${alias}_${properties.propertyName}`

View File

@ -46,7 +46,7 @@ describe('paginate', () => {
database: ':memory:', database: ':memory:',
}), }),
synchronize: true, synchronize: true,
logging: false, logging: ['error'],
entities: [CatEntity, CatToyEntity, CatHomeEntity, CatHomePillowEntity], entities: [CatEntity, CatToyEntity, CatHomeEntity, CatHomePillowEntity],
}) })
await dataSource.initialize() await dataSource.initialize()

View File

@ -126,7 +126,10 @@ export async function paginate<T extends ObjectLiteral>(
// relations: ["relation"] // relations: ["relation"]
if (Array.isArray(config.relations)) { if (Array.isArray(config.relations)) {
config.relations.forEach((relation) => { config.relations.forEach((relation) => {
queryBuilder.leftJoinAndSelect(`${queryBuilder.alias}.${relation}`, `${queryBuilder.alias}_${relation}`) queryBuilder.leftJoinAndSelect(
`${queryBuilder.alias}.${relation}`,
`${queryBuilder.alias}_${relation}_rel`
)
}) })
} else { } else {
// relations: {relation:true} // relations: {relation:true}
@ -141,11 +144,15 @@ export async function paginate<T extends ObjectLiteral>(
queryBuilder.leftJoinAndSelect( queryBuilder.leftJoinAndSelect(
`${alias ?? prefix}.${relationName}`, `${alias ?? prefix}.${relationName}`,
`${alias ?? prefix}_${relationName}` `${alias ?? prefix}_${relationName}_rel`
) )
if (typeof relationSchema === 'object') { if (typeof relationSchema === 'object') {
createQueryBuilderRelations(relationName, relationSchema, `${alias ?? prefix}_${relationName}`) createQueryBuilderRelations(
relationName,
relationSchema,
`${alias ?? prefix}_${relationName}_rel`
)
} }
}) })
} }