question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Duplicate column in getMany with join + pagination

See original GitHub issue

Bug 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:open
  • Created a year ago
  • Reactions:2
  • Comments:12

github_iconTop GitHub Comments

6reactions
jongomescommented, Jun 2, 2022

image temporary solution

1reaction
superiumscommented, Sep 12, 2022

This is because in typeorm-service.ts: the select params return two id:

getSelect(query, options) {
        const allowed = this.getAllowedColumns(this.entityColumns, options);
        const columns = query.fields && query.fields.length
            ? query.fields.filter((field) => allowed.some((col) => field === col))
            : allowed;
        const select = [
            ...(options.persist && options.persist.length ? options.persist : []),
            ...columns,
            ...this.entityPrimaryColumns,
        ].map((col) => `${this.alias}.${col}`);
        console.log('-------->select',select)
        console.log('-------->columns',columns)
        console.log('-------->entityPrimaryColumns',this.entityPrimaryColumns)
        return select;
    }

one from allowed, one from this.entityPrimaryColumns.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Row num is not working for duplicate columns while pagination
Firstly, your query is incorrect since you have ambiguously defined the columns. It will throw ORA-00918: column ambiguously defined .
Read more >
java - HQL pagination returns same result on different pages
The workaround you found is a good one, use it - if your main ordering is by non-unique column, add a unique column...
Read more >
EFFECTIVE PAGINATION IN ELASTICSEARCH - LinkedIn
The paging request can end in returning duplicate records, inconsistently ordered records, or even result in catastrophic failure for an ...
Read more >
Select using Query Builder - typeorm - GitBook
​Joining and mapping functionality​ ... ​Hidden Columns​. ​Querying Deleted rows​ ... If you are using pagination, it's recommended to use take instead.
Read more >
Duplicate rows are in the view's result set (was: distinct doesn't ...
Same problem here. Getting duplicates when sorting by a field with multiple values. The workaround using a relationship as described in the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found