[英]how make a query in sequelize with a OR condition when that field is the foreignkey
我在续集中需要有关此查询的一些帮助,此查询完全按照我的意愿工作,但我不知道如何执行该 OR 条件:
select * from
core."Customers" c
inner join core."Townhouses" t
on t.id = c.townhouseid
inner join portal."TownhouseFreeFreights" tf
on tf.townhouseid = t.id
or tf.townhouseid is null
where c.id = 1
and tf."minValue" >= 0
我的问题是那部分:或者 tf.townhouseid 是 null
我应该在这里做什么:
getFreightRules(){
return Customer.findAll({
include: [{
model: Townhouse,
require: true,
include:[{
model: TownhouseFreeFreights,
}]
}]
})
}
像这样的东西应该可以工作( https://sequelize.org/master/manual/model-querying-basics.html#examples-with--code-op-and--code--and--code-op-or- -代码- )
getFreightRules(){
return Customer.findAll({
include: [{
model: Townhouse,
require: true,
include:[{
model: TownhouseFreeFreights,
where: {
[Op.or]: [
{ tf.townhouseid: t.id },
{ tf.townhouseid: null }
]
}
}]
}]
})
}
或者
getFreightRules(){
return Customer.findAll({
include: [{
model: Townhouse,
require: true,
include:[{
model: TownhouseFreeFreights,
where: {
tf.townhouseid: {
[Op.or]: [t.id, null]
}
}
}]
}]
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.