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 {
if (isRelation) {
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) {
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 {
return `${alias}_${properties.propertyPath}_rel_${properties.propertyName}`
}
} else {
return `${alias}_${properties.propertyPath}.${properties.propertyName}`
return `${alias}_${properties.propertyPath}_rel.${properties.propertyName}`
}
} else if (isVirtualProperty) {
return query ? `(${query(`${alias}`)})` : `${alias}_${properties.propertyName}`

View File

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

View File

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