我有一个 photo
模型和 tags
Sequelize 中的模型。我使用以下查询显示
models.photo
.findAll({
limit: 50,
offset: ctx.offset,
include :[{
model: models.tags,
as: 'tags'
},{
model: models.description,
as: 'desc'
}]
})
.then((photos) => {
ctx.res.send(photos);
});
我的问题,如何过滤包含特定标签的照片?它们使用关联
models.photo.hasMany(models.tags, {foreignKey: 'photo', sourceKey: 'name'});
例如,如果用户想要过滤 鸭子那么我应该能够显示鸭子的所有照片以及与它们相关的所有标签吗?
最佳答案
您可以内部加入photo
和 tags
然后过滤 tags
按你的属性
models.photo
.findAll({
limit: 50,
offset: ctx.offset,
include :[{
model: models.tags,
as: 'tags',
where: {
tag_name: 'duck'
},
required: true
}]
})
.then((photos) => {
ctx.res.send(photos);
});
关于node.js - 过滤包含在 sequelize 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51056500/