如何使用别名包含关联通过 WHERE 执行 FindAll
现在我有这个代码:
Photobook.findAndCountAll({
include: [
{
attributes: ['full_name'],
as: 'layouter_name',
model: UserModel,
},
{
attributes: ['full_name'],
as: 'current_pic',
model: UserModel,
}
],
where:{
{
'$current_pic.full_name$' : {
$like: '%name%'
}
}
}
});
它不起作用,因为 Photobook 模型没有 current_pic.full_name
字段。我只想搜索 current_pic
关联而不是 layouter_name
关联。
如何执行类似的操作?
最佳答案
试试这个:
Photobook.findAndCountAll({
include: [
{
attributes: ['full_name'],
as: 'layouter_name',
model: UserModel,
},
{
attributes: ['full_name'],
as: 'current_pic',
model: UserModel,
where: {
full_name: {
$like: '%name%'
}
},
required: true
}
]
});
您可以在 include
数组本身中给出 where 条件。 required: true
确保 JOIN
被视为 INNER JOIN
关于mysql - FindAll WHERE 在 Sequelize 中使用包含别名字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46167937/