使用 sequelize N:M 关联实现 "flat" 结果的 "correct" 方法是什么
What is the "correct" way to realize a "flat" result using sequelize N:M associations
我有一个产品数据库,其中包含一些具有 1-n 个类别的产品。我的产品存储在 products
table 中,类别存储在 categories
中,关联存储在 productCategoryRel
中。现在我的愿望是,当我使用 Product.find()
获得产品时,接收包含 categories
属性 的结果,其中包含来自 categories
table 的数据,没有任何其他嵌套属性。
目前我得到的是:
{
"id": 1,
"title": "Testproduct",
"categories": [
{
"id": 1,
"title": "Testcategory",
"productCategoryRel": {
"category_id": 1,
"product_id": 1
}
}
]
}
所以我的 product
包含 属性 categories
这很好,但是 categories
还包含 productCategoryRel
table 中的所有数据] 我想避免的。我已经像地狱一样用谷歌搜索,研究了文档等等,我不确定我是否有可能实现我想要实现的目标。
这就是我定义(测试)模型的方式:
let ProductCategoryRel = sequelize.define('productCategoryRel', {});
let Category = sequelize.define('category', {
title: Sequelize.STRING
});
let Product = sequelize.define('product', {
title: Sequelize.STRING
}, {
defaultScope: {
include: [{
model: Category
}]
}
});
以及协会:
Product.belongsToMany(Category, {
through: ProductCategoryRel,
foreignKey: 'product_id',
});
Category.belongsToMany(Product, {
through: ProductCategoryRel,
foreignKey: 'category_id',
});
ProductCategoryRel.hasMany(Product, {foreignKey: 'id'});
ProductCategoryRel.hasMany(Category, {foreignKey: 'id'});
我想要的结果很简单:
{
"id": 1,
"title": "Testproduct",
"categories": [
{
"id": 1,
"title": "Testcategory"
}
]
}
这有可能吗?我需要如何改变我的联想?我已经尝试了一些奇怪的 hasMany/belongsTo 组合,但没有成功。
我正在使用 Sequelize 3.12.1。
尝试这样的事情(可能有一些错误,但如果你给我反馈,我们会解决问题):
Product.findOne({
where: {
id: yourProductId
},
include: [{
model : Category,
order: [['id','desc']],
through: {attributes: []}
}],
})
attributes
属性 允许您 select 显示什么数据。它也可以用于包含的模型。
文档中有更多内容 http://docs.sequelizejs.com/en/latest/docs/querying/
我有一个产品数据库,其中包含一些具有 1-n 个类别的产品。我的产品存储在 products
table 中,类别存储在 categories
中,关联存储在 productCategoryRel
中。现在我的愿望是,当我使用 Product.find()
获得产品时,接收包含 categories
属性 的结果,其中包含来自 categories
table 的数据,没有任何其他嵌套属性。
目前我得到的是:
{
"id": 1,
"title": "Testproduct",
"categories": [
{
"id": 1,
"title": "Testcategory",
"productCategoryRel": {
"category_id": 1,
"product_id": 1
}
}
]
}
所以我的 product
包含 属性 categories
这很好,但是 categories
还包含 productCategoryRel
table 中的所有数据] 我想避免的。我已经像地狱一样用谷歌搜索,研究了文档等等,我不确定我是否有可能实现我想要实现的目标。
这就是我定义(测试)模型的方式:
let ProductCategoryRel = sequelize.define('productCategoryRel', {});
let Category = sequelize.define('category', {
title: Sequelize.STRING
});
let Product = sequelize.define('product', {
title: Sequelize.STRING
}, {
defaultScope: {
include: [{
model: Category
}]
}
});
以及协会:
Product.belongsToMany(Category, {
through: ProductCategoryRel,
foreignKey: 'product_id',
});
Category.belongsToMany(Product, {
through: ProductCategoryRel,
foreignKey: 'category_id',
});
ProductCategoryRel.hasMany(Product, {foreignKey: 'id'});
ProductCategoryRel.hasMany(Category, {foreignKey: 'id'});
我想要的结果很简单:
{
"id": 1,
"title": "Testproduct",
"categories": [
{
"id": 1,
"title": "Testcategory"
}
]
}
这有可能吗?我需要如何改变我的联想?我已经尝试了一些奇怪的 hasMany/belongsTo 组合,但没有成功。
我正在使用 Sequelize 3.12.1。
尝试这样的事情(可能有一些错误,但如果你给我反馈,我们会解决问题):
Product.findOne({
where: {
id: yourProductId
},
include: [{
model : Category,
order: [['id','desc']],
through: {attributes: []}
}],
})
attributes
属性 允许您 select 显示什么数据。它也可以用于包含的模型。
文档中有更多内容 http://docs.sequelizejs.com/en/latest/docs/querying/