2
我有两个型号:链接一个Sequelize模型到两个不同的领域,从另一个模型
BusinessEntity
和Invoice
我想Invoice
有一个seller
和buyer
,两者都指向BusinessEntity
记录。所以,我希望在invoices
表(Invoice
型号)有两个字段:seller_id
和buyer_id
。
所以,我做了这样的事情:
BusinessEntity
const BusinessEntity = sequelize.define("business_entity", {
display_name: Sequelize.STRING
});
Invoice
const Invoice = sequelize.define("invoice", {
series: Sequelize.STRING,
number: Sequelize.INTEGER,
...
});
// Indeed, this creates the seller_id and buyer_id fields in the invoice
Invoice.belongsTo(BusinessEntity, { as: "seller" });
Invoice.belongsTo(BusinessEntity, { as: "buyer" });
似乎不自然的我打电话给Invoice.belongsTo(BusinessEntity)
,但如果我这样做:
BusinessEntity.belongsTo(Invoice, { as: "seller" });
BusinessEntity.belongsTo(Invoice, { as: "buyer" });
... seller_id
和buyer_id
列根本不创建。为什么?
我成功插入的发票,从business_entities
表assiging IDS。
我该如何查询Invoice
当我想include
seller
和buyer
在输出?
我想:
Invoice.findOne({ include: [BusinessEntity] })
但是失败,此错误:
business_entity is not associated to invoice!
什么是做到这一点的正确方法?如何解决问题?