Sequelize 查询 Or-ing where 和 include 语句 [英] Sequelize query Or-ing where and include statements
问题描述
我正在尝试进行 Sequelize 查询,该查询仅返回与 where 子句 或 include wheres 匹配的记录.例如,我有一个属于人员模型的用户模型.用户模型有一个名为 username 的字段,而人员模型有一个名字和姓氏.
I am trying make a Sequelize query that returns only records that match a where clause or include wheres. For example I have a user model that belongs to a person model. The user model has one field called username and the person model has a first and last name.
示例查询:
{
"where": {
"username": {
"$iLike": "%s%"
}
},
"include": [{
"model": Person,
"where": {
"$or": [{
"firstName": {
"$iLike": "%s%"
}
}, {
"lastName": {
"$iLike": "%s%"
}
}]
}
}]
}
上面的查询匹配用户名和(名字或姓氏)匹配ilike"'s'的记录.我正在尝试实现用户名或(名字或姓氏).
The query above matches records that have a username AND (firstname or lastname) matching "ilike" 's'. I am trying to achieve username OR (firstname or lastname).
我知道在 where 内工作时如何使用 Or 运算符,但我想在 where 或 include 上使用 Or.这可能吗?我使用必需的假吗?
I know how do use Or operators when working inside of a where but I want to use an Or on where or include. Is this possible? Do I use required false?
推荐答案
试试这个:
{
where: {
$or: [
{
userName: {
$ilike: "%s%"
}
},
{
'$person.firstName$': {
$ilike: "%s%"
}
},
{
'$person.lastName$': {
$ilike: "%s%"
}
}
]
},
include: [
{
model: Person,
}
]
}
这篇关于Sequelize 查询 Or-ing where 和 include 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!