[英]Sequelize querying both parent model and included model using [Op.or]
I want to findAll Bookings
where the booking has been paid.我想查找已支付Bookings
的所有预订。
A legacy booking has been paid if the paymentAuthorised: boolean
attribute on the Booking
table is true.如果Booking
表上的paymentAuthorised: boolean
属性为真,则已支付旧版预订。
A new booking has been paid if the paymentAuthorised: boolean
attribute on the Payment
table is true and type: string
attribute on the Payment
table is 'booking'.如果paymentAuthorised: boolean
属性在Payment
表上为 true 并且type: string
属性在Payment
表上是“booking”,则新的预订已经支付。
When I perform the following query it returns an error saying payments.paymentAuthorised not found.当我执行以下查询时,它返回一个错误,提示未找到 payment.paymentAuthorised。
const bookings = await db.Booking.findAll({
include: [
{
model: db.Payment,
as: "payments",
},
],
where: {
[Op.or]: [
{
paymentAuthorised: { [Op.eq]: true },
},
{
"$payments.paymentAuthorised$": { [Op.eq]: true },
"$payments.type$": { [Op.eq]: "booking" },
},
],
},
order: [["dateTime", "asc"]],
});
I worked this issue out in the end by logging the generated SQL.我最终通过记录生成的 SQL 解决了这个问题。 You need to add the following parameter: subQuery: false
.您需要添加以下参数: subQuery: false
。 This is so it generates an SQL query which includes the joins before the where.这样它会生成一个 SQL 查询,其中包括 where 之前的连接。
const bookings = await db.Booking.findAll({
include: [
{
model: db.Payment,
as: "payments",
},
],
where: {
[Op.or]: [
{
paymentAuthorised: { [Op.eq]: true },
},
{
"$payments.paymentAuthorised$": { [Op.eq]: true },
"$payments.type$": { [Op.eq]: "booking" },
},
],
},
subQuery: false,
order: [["dateTime", "asc"]],
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.