当该字段是外键时,如何使用 OR 条件进行后续查询
how make a query in sequelize with a OR condition when that field is the foreignkey
我在 sequelize 中需要一些关于这个查询的帮助,这个查询完全按照我想要的方式工作,但我不知道如何做那个或条件:
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为空
我应该在这里做什么:
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--code-)
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]
}
}
}]
}]
})
}
我在 sequelize 中需要一些关于这个查询的帮助,这个查询完全按照我想要的方式工作,但我不知道如何做那个或条件:
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为空
我应该在这里做什么:
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--code-)
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]
}
}
}]
}]
})
}