No entity column was found using typeorm find

Viewed 6713

I am trying to find() a repository using a condition from its relation.

(node:16719) UnhandledPromiseRejectionWarning: EntityColumnNotFound: No entity column "order.paid" was found.

The relations are as follows:

Order.ts

@Entity('orders')
export class Order extends BaseModel {
  @PrimaryGeneratedColumn('uuid')
  @ApiProperty({ description: 'The Order ID' })
  id: string;

  // The Payment is finished or not
  @Column('boolean', { nullable: false, default: false })
  @ApiProperty({ description: 'Payment Confirmation for Database side' })
  paid: boolean;


  // The Items
  @OneToMany(type => Item, item => item.order, { nullable: false, eager: true, cascade: ['insert', 'update', 'remove'] })
  @ApiProperty({ description: 'The Order Items', isArray: true, type: Item })
  items: Item[];
}

Item.ts

export class Item extends BaseModel {
  @PrimaryGeneratedColumn('uuid')
  @ApiProperty({ description: 'The Item UUID' })
  id: string;

  @ManyToOne(type => Order, order => order.items, { nullable: false })
  @JoinColumn()
  order: Order;

  // The Shipping is finished or not
  @ApiProperty({ description: 'Shipped Indicator' })
  @Column('boolean', { nullable: false, default: false })
  shipped: boolean;
}

The find code as follows:

await this.payments.items.find({ where: { createdAt: Between(from, to), 'order.paid': true, shipped: true }, order: { createdAt: 'ASC' } });

I tried the following variations:

await this.payments.items.find({ where: { createdAt: Between(from, to), order:{ paid:true}, shipped: true }, order: { createdAt: 'ASC' } });
 const items = await this.payments.items.createQueryBuilder("item")
     .leftJoinAndSelect("order.paid","order")
     .where("order.paid = :paid AND item.shipped = :shipped", { paid:true , shipped: true})
     .getMany();

but none of them work,

I understand that changing the Item repository to eager evaluation might make it work but typeorm disables circular eager declaration.

1 Answers

This seems to have been fixed in the past month (with some minor kinks still being worked out) : https://github.com/typeorm/typeorm/issues/2707

This should work

await this.payments.items.find({ where: { createdAt: Between(from, to), order:{ paid:true}, shipped: true }, order: { createdAt: 'ASC' } });

Foreign keys do not appear to work: https://github.com/typeorm/typeorm/issues/3288

Note: if from/to are supposed to be dates and not timestamps, you'll want to use date-fn's startOfDay(x) & endOfDay(x)

Related