【发布时间】:2016-07-27 07:49:50
【问题描述】:
我有两个模型,它们之间有很多对多的关联(我使用的是sails.js 框架)。我在关联表中添加了添加字段。我想填充那个添加字段。我如何实现这一目标?我的模型如下:
//Store.js file
module.exports = {
autoCreatedAt: false,
autoUpdatedAt: false,
attributes: {
name: "string",
slug: "string",
imageURL: "string",
termsAndConditions: "string",
link: "string",
productID: {
collection: 'product', //This is for association with the product model
via: 'storeID',
through: 'price'
}
}
};
下面是我的 Product.js 文件
//Product.js
module.exports = {
autoCreatedAt: false,
autoUpdatedAt: false,
attributes: {
name: 'string',
storeID: {
collection: 'stores',
via: 'productID', //This is for association with the Store model
through: 'price'
}
}
};
下面是我的模型 Price.js
module.exports = {
autoCreatedAt: false,
autoUpdatedAt: false,
attributes: {
storeID: {
model: 'stores'
},
productID: {
model: 'product'
},
price: 'integer' //I want to populate this additional field when calling api '/product' or '/store'
}
};
如何通过调用 api '/product' 或 '/store' 填充 Price 表的附加字段 price?
【问题讨论】: