获取没有子关系或具有某些参数的子项的项目

时间:2020-11-10 03:27:53

标签: typeorm

我如何不进行任何付款或状态待定的付款?

Laravel上的“ whereDoesntHave”之类的东西

谢谢!

@Entity()
export class Booking extends BaseEntity{
                
    @Column({type: 'varchar', length: 10})
    code: string

    @OneToMany(() => Payment, Payment => Payment.booking, { cascade: true })
    payments: Payment

}

@Entity()
export class Payment extends BaseEntity {
    
  @ManyToOne(() => Booking, { nullable: false })
  booking: Booking
  
  @Column({ type: "float" })
  amount: number
  
  @Column()
  status: PaymentStatus;
    
}

1 个答案:

答案 0 :(得分:0)

首先,您应该向名为Payment的{​​{1}}实体添加外键:

bookId

然后,您应该获得@Entity() export class Payment extends BaseEntity { @Column({nullable: true}) bookId:number; @ManyToOne(() => Booking, { nullable: false }) @JoinColumn({ name: 'bookId' }) // <= and add JoinColumn here booking: Booking @Column({ type: "float" }) amount: number @Column() status: PaymentStatus; } bookId为空的所有预订:

Payment
await Booking.find({ relations:['payments'], where:[ 'payments.status': 'pending', bookId: null ] }) 中的

[]代表查询中的where