嵌套包含在sequelize中?
Nested include in sequelize?
如何进行嵌套包含?
我有 table 个产品与评论有一对多关系,table 个评论与用户 table 有多对一关系。
所以评论有user_id,和product_id。
我的代码是这样的
var models = require('../models');
models.products.findAll({
include: [
{model: models.comments}
]
}).then(function (products) {
next(products);
}).catch(function (err) {
next(err);
});
});
我收到了评论,但我想要一些类似的东西
models.products.findAll({
include: [
{model: models.comments.include(models.comments.users)}
]
})
这是否可以不编写自定义查询?
models.products.findAll({
include: [
{model: models.comments, include: [models.comments.users] }
]
})
您可以使用 include
.
例如:
Category = sequelize.define(...);
Product = sequelize.define(...);
Product.belongsTo(Category, {foreignKey: 'fk_category'});
Product.findAll({
include: [{
model: Category
}]
});
将在 category
属性.
中检索嵌套类别,而不仅仅是 ID
提供的解决方案对我不起作用,这是我使用的打字稿版本并猜测续集版本:
// sequelize-typescript
models.products.findAll({
where,
include: [{
model: Comment,
include: [User]
}]
});
// without typescript (guessing here)
models.products.findAll({
where,
include: [{
model: models.comments,
include: [{
model: models.users
}]
}]
});
有个方法很简洁明了! https://sequelize.org/docs/v6/advanced-association-concepts/eager-loading/#including-everything
// Fetch all models associated with User
User.findAll({ include: { all: true }});
// Fetch all models associated with User and their nested associations (recursively)
User.findAll({ include: { all: true, nested: true }});
如果这对以后的人有帮助:
我嵌套了多个层次,并且不断收到 Typescript 错误,我的调用与任何覆盖都不匹配。
对我来说,在之前的答案(或文档)中并不清楚您 必须 将 include
对象包装在所有级别的数组中(甚至在你只提供 1 个对象)。
作品:
User.findAll({
include: [{
model: Contact,
include: [{
model: Address,
include: [{
model: City,
include: [State, Country]
}]
}]
}]
});
不工作:
User.findAll({
include: { // <-- notice the missing square bracket
model: Contact,
include: { // <-- notice the missing square bracket
model: Address,
include: { // <-- notice the missing square bracket
model: City,
include: [State, Country]
} // <-- notice the missing square bracket
} // <-- notice the missing square bracket
} // <-- notice the missing square bracket
});
如何进行嵌套包含? 我有 table 个产品与评论有一对多关系,table 个评论与用户 table 有多对一关系。 所以评论有user_id,和product_id。 我的代码是这样的
var models = require('../models');
models.products.findAll({
include: [
{model: models.comments}
]
}).then(function (products) {
next(products);
}).catch(function (err) {
next(err);
});
});
我收到了评论,但我想要一些类似的东西
models.products.findAll({
include: [
{model: models.comments.include(models.comments.users)}
]
})
这是否可以不编写自定义查询?
models.products.findAll({
include: [
{model: models.comments, include: [models.comments.users] }
]
})
您可以使用 include
.
例如:
Category = sequelize.define(...);
Product = sequelize.define(...);
Product.belongsTo(Category, {foreignKey: 'fk_category'});
Product.findAll({
include: [{
model: Category
}]
});
将在 category
属性.
提供的解决方案对我不起作用,这是我使用的打字稿版本并猜测续集版本:
// sequelize-typescript
models.products.findAll({
where,
include: [{
model: Comment,
include: [User]
}]
});
// without typescript (guessing here)
models.products.findAll({
where,
include: [{
model: models.comments,
include: [{
model: models.users
}]
}]
});
有个方法很简洁明了! https://sequelize.org/docs/v6/advanced-association-concepts/eager-loading/#including-everything
// Fetch all models associated with User
User.findAll({ include: { all: true }});
// Fetch all models associated with User and their nested associations (recursively)
User.findAll({ include: { all: true, nested: true }});
如果这对以后的人有帮助:
我嵌套了多个层次,并且不断收到 Typescript 错误,我的调用与任何覆盖都不匹配。
对我来说,在之前的答案(或文档)中并不清楚您 必须 将 include
对象包装在所有级别的数组中(甚至在你只提供 1 个对象)。
作品:
User.findAll({
include: [{
model: Contact,
include: [{
model: Address,
include: [{
model: City,
include: [State, Country]
}]
}]
}]
});
不工作:
User.findAll({
include: { // <-- notice the missing square bracket
model: Contact,
include: { // <-- notice the missing square bracket
model: Address,
include: { // <-- notice the missing square bracket
model: City,
include: [State, Country]
} // <-- notice the missing square bracket
} // <-- notice the missing square bracket
} // <-- notice the missing square bracket
});