Duplicate column in getMany with join + pagination
See original GitHub issueBug Report
getMany throws SQL error when join, limit is used together
it’s only throwing an error when trying to get relational data with pagination
Current behavior
throwing SQL error: Duplicate column name:-
“SELECT DISTINCT ‘distinctAlias’.‘Roles_id’ AS ‘ids_Roles_id’ FROM (SELECT 'Roles'.'id' AS 'Roles_id', 'Roles'.'id' AS 'Roles_id', 'Roles'.
‘createdAt’ AS ‘Roles_createdAt’, ‘Roles’.‘updatedAt’ AS ‘Roles_updatedAt’, ‘Roles’.‘name’ AS ‘Roles_name’, ‘users’.‘id’ AS ‘users_id’, ‘users’.‘id’ AS ‘users_id’, ‘users’.‘name’ AS ‘users_name’, ‘users’.‘email’ AS ‘users_email’ FROM ‘Roles’ ‘Roles’ LEFT JOIN ‘user-roles’ ‘users_Roles’ ON ‘users_Roles’.‘Role’=‘Roles’.‘id’ LEFT JOIN ‘User’ ‘users’ ON ‘users’.‘id’=‘users_Roles’.‘User’) ‘distinctAlias’ ORDER BY ‘Roles_id’ ASC LIMIT 2”,
Input Code
/roles?join=users&limit=2&page=1
Expected behavior
should return
{
"data": [
{
"id": 1,
"createdAt": "2022-05-27T07:45:29.435Z",
"updatedAt": "2022-05-27T07:45:29.435Z",
"name": "admin",
"users": [
{
"id": 1,
"createdAt": "2022-05-27T07:45:59.381Z",
"updatedAt": "2022-05-27T07:45:59.381Z",
"name": "admin",
"phone": "9876543210",
"email": "admin@gmail.com",
"isActive": false
}
]
},
],
"count": 1,
"total": 6,
"page": 1,
"pageCount": 6
}
Possible Solution
Tested
It’s working with TypeOrm version <= 0.2.45
Reason
maybe because in typeOrm version 0.3.0 Deprecated way of loading entity relations:
Old way of loading entity relations:
userRepository.find({
select: ["id", "firstName", "lastName"]
})
New way of loading entity relations:
userRepository.find({
select: {
id: true,
firstName: true,
lastName: true,
}
})
Environment
Package version: 5.0.0-alpha.3
For Tooling issues:
- Node version: v16.15.0
- Platform: Linux
- Database MySQL
Others:
"typeorm": "^0.3.6"
"@nestjs/typeorm": "^8.0.3",
## Repository with minimal reproduction
Issue Analytics
- State:
- Created a year ago
- Reactions:2
- Comments:12
This is because in typeorm-service.ts: the select params return two id:
one from allowed, one from this.entityPrimaryColumns.