Typeorm获取ID的数组列的关系

时间:2019-09-03 15:20:49

标签: postgresql typeorm

我希望将id的一列添加到发票中的项目。{p>

我遇到错误

  

未找到Invoice#invoiceItems的实体元数据。

我尝试了以下两种选择:

Invoice

class Invoice {

  @OneToMany(type => InvoiceItem, invoiceItem => invoiceItem.id)
  @JoinColumn()
  invoiceItems: InvoiceItem[]
  @RelationId((self: Invoice) => self.invoiceItems)
  invoiceItemIds: number[]

}

如何为ID数组添加关系?

1 个答案:

答案 0 :(得分:0)

您不能将@JoinColumn用于@OneToMany(请参见docs),需要在InvoiceItem类中进行配置:

@Entity()
class Invoice { 
    @OneToMany(type => InvoiceItem, invoiceItem => invoiceItem.invoice) 
    invoiceItems: InvoiceItem[];

    @RelationId((self: Invoice) => self.invoiceItems) 
    invoiceItemIds: number[] 
}

@Entity()
class InvoiceItem { 
    ...
    @ManyToOne(type => Invoice, invoice => invoice.invoiceItems) 
    invoice: Invoice;
    ...
}

我还没有使用@RelationId,但是如果您正确加入InvoiceItems,这应该可以工作。