nestjsx/crud typeorm relations query

Viewed 336

How do I have to write the query in order to get relations of the entity populated with?

Checked the below options, but none of them works.

  • localhost:3000/reservations?join=concert_id
  • localhost:3000/reservations?join=concert

If I change the concert_id key within the join object of @Crud() options to concert (as the name of the property in the Reservation.entity.ts file is just concert, not concert_id), then I get an error error: column reference "Reservation_id" is ambiguous.

Any help is much appreciated. Thanks in advance!

Reservation.entity.ts

@Entity('reservations')
export class Reservation {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ name: 'booking_reference' })
  bookingReference: string;

  @ManyToOne(() => Concert, (concert) => concert.reservations)
  @JoinColumn({ name: 'concert_id' })
  concert: Concert;
}

Concert.entity.ts

@Entity('concerts')
export class Concert {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;


  @OneToMany(() => Reservation, (reservation) => reservation.concert)
  reservations: Reservation[];
}

Reservation.controller.ts

@Crud({
  model: {
    type: Reservation
  },
  dto: {
    create: CreateReservationDto
  },
  validation: { always: true },
  query: {
    join: {
      concert_id: {
        eager: true,
        alias: "concert"
      }
    }
  }
})
@ApiTags('Reservation')
@Controller('reservation')
export class ReservationController implements CrudController<Reservation> {
  constructor(public service: ReservationService) {}
0 Answers
Related