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.

Limit/offset not working in combination with joins with Postgres DB

See original GitHub issue

I’m getting error QueryFailedError: column reference "Ticket_id" is ambiguous when trying to query findMany with both a join and a specified limit when using Postgres DB. Tried the same code, but with a SQLite DB and it works as intended.

Any suggestions on how to get this to work with Postgres?

Error

QueryFailedError: column reference "Ticket_id" is ambiguous
    at PostgresQueryRunner.query (/Users/jonasnr/WebstormProjects/nest_test/src/driver/postgres/PostgresQueryRunner.ts:299:19)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at SelectQueryBuilder.loadRawResults (/Users/jonasnr/WebstormProjects/nest_test/src/query-builder/SelectQueryBuilder.ts:3519:25)
    at SelectQueryBuilder.getRawMany (/Users/jonasnr/WebstormProjects/nest_test/src/query-builder/SelectQueryBuilder.ts:1551:29)
    at SelectQueryBuilder.executeEntitiesAndRawResults (/Users/jonasnr/WebstormProjects/nest_test/src/query-builder/SelectQueryBuilder.ts:3213:26)
    at SelectQueryBuilder.getRawAndEntities (/Users/jonasnr/WebstormProjects/nest_test/src/query-builder/SelectQueryBuilder.ts:1595:29)
    at SelectQueryBuilder.getMany (/Users/jonasnr/WebstormProjects/nest_test/src/query-builder/SelectQueryBuilder.ts:1685:25)

Query

'SELECT DISTINCT distinctAlias.Ticket_id as "ids_Ticket_id" FROM (SELECT Ticket.id AS Ticket_id, Ticket.id AS Ticket_id, Ticket.ticketName AS Ticket_ticketName, user.id AS user_id, user.id AS user_id, user.owner AS user_owner, user.username AS user_username, user.scope AS user_scope, user.ticketId AS user_ticketId FROM ticket Ticket LEFT JOIN user user ON user.ticketId=Ticket.id) distinctAlias ORDER BY Ticket_id ASC LIMIT 1'

See all files below:

Service

@Injectable()
export class TicketService extends TypeOrmCrudService<Ticket> {
  constructor(@InjectRepository(Ticket) repo) {
    super(repo);
  }
}

Controller

@ApiTags('tickets')
@Crud({
  model: { type: Ticket },
  query: {
    join: {
      user: { eager: true },
    },
    limit: 1,
  },
})
@Controller('ticket')
export class TicketController implements CrudController<Ticket> {
  constructor(public service: TicketService) {}
}

Entities

@Entity('ticket')
export class Ticket {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  ticketName: string;

  @OneToOne(() => User, (user) => user.ticket)
  user: User;
}
@Entity('user')
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  owner: string;

  @Column()
  username: string;

  @Column()
  scope: string;

  @OneToOne(() => Ticket)
  @JoinColumn()
  ticket: Ticket;
}

Module

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'postgres',
      host: 'localhost',
      port: 5432,
      username: 'postgres',
      password: 'postgres',
      database: 'postgres',
      autoLoadEntities: true,
      synchronize: true,
    }),
    TicketModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {
  constructor() {}
}

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:7
  • Comments:16

github_iconTop GitHub Comments

10reactions
5andi5commented, Sep 29, 2022

Maybe this patch helps someone:

import { uniq } from 'lodash'

@Injectable()
export class MyEntityService extends TypeOrmCrudService<MyEntity> {
  getSelect(query, options) {
    return uniq(super.getSelect(query, options))
  }
}
5reactions
hoangnq3004commented, Apr 6, 2022

+1

P/S: When I downgrade typeorm to 0.2.45 the problem has solved

Read more comments on GitHub >

github_iconTop Results From Across the Web

sql - How to use LIMIT and OFFSET when joining one to many ...
I am using sqlalchemy core with a postgres database ...
Read more >
LIMIT and OFFSET Can Work with JOIN | by Dan Crews
One of the SQL issues I probably hear the most is some variation of “I added a LIMIT [and/or OFFSET] to my query,...
Read more >
PostgreSQL - LIMIT with OFFSET clause - GeeksforGeeks
The PostgreSQL LIMIT clause is used to get a subset of rows generated by a query. It is an optional clause of the...
Read more >
Documentation: 15: SELECT - PostgreSQL
If ORDER BY is not given, the rows are returned in whatever order the system ... Cartesian product (i.e., all combined rows that...
Read more >
PostgreSQL NATURAL JOIN | Examples to ... - eduCBA
A Join operation actually creates a temporary table for a set of rows to work on two or more tables. The tables defined...
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