本文介绍了hasMany关系:包括从另一个方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有下一个模型:
user.json: {//... "relations":{ "invoices": { "type": "hasMany", "model": "Invoice", "foreignKey": "receiverId" }, } //... }又名用户可能有很多发票.这段代码将字段ReceiverId添加到发票模型中.
A.k.a. a user might have many invoices. This code adds the field receiverId to the invoice model.
现在,我想获取一张发票清单,包括其收货人.我该怎么办?
Now I want to get a list of invoices including their receivers. How can I do that?
Invoice.find({include: "reciever"})或
Invoice.find({include: "user"})不起作用,返回:未为发票模型定义关系\"接收器\"错误.
Did not work, returned: "Relation \"receiver\" is not defined for Invoice model" error.
感谢您的帮助.
推荐答案您必须定义在您的帐单模型中属于属于" 关系.
invoice.json:
invoice.json:
{//... "relations":{ "receiver": { "type": "belongsTo", "model": "Receiver" }, } //... }然后您可以像这样查询模型:
Then you can query your model like this:
Invoice.find({include: "receiver"}, function(data){ console.log(data); });更多推荐
hasMany关系:包括从另一个方向
发布评论