下面有一个动态where clause
排序的过滤器查询,
let ticketnotificationsWhere = {};
if (req.query.department) {
ticketnotificationsWhere.department = req.query.department;
}
if (req.query.ticketid) {
ticketnotificationsWhere.ticketid = req.query.ticketid;
}
let ticketWhere = {};
if (req.query.isactionable) {
ticketWhere.isactionable = req.query.isactionable;
}
if (req.query.maintypeid) {
ticketWhere.maintypeid = req.query.maintypeid;
}
if (req.query.typeid) {
ticketWhere.typeid = req.query.typeid;
}
if (req.query.subtypeid) {
ticketWhere.subtypeid = req.query.subtypeid;
}
let ticketdetailsWhere = {};
if (req.query.customerid) {
ticketdetailsWhere.customerid = req.query.customerid;
}
return ticketdb.ticketnotifications.findAndCountAll({
limit: req.query.pageSize,
offset: req.query.page,
where:ticketnotificationsWhere,
attributes: ['id', 'ticketid', 'createdby', 'createddate', 'status'],
include:[{
model:ticketdb.tickets,
required: true,
where:ticketWhere,
attributes: ['isactionable'],
include:[{
model:ticketdb.ticketdetails,
required: true,
where:ticketdetailsWhere,
attributes: ['poid', 'productid', 'shippingreferenceno', 'customerid', 'issueqty']
},
{
model:ticketdb.maintypemaster,
attributes: [['name', 'maintypename']]
},
{
model:ticketdb.typemaster,
attributes: [['name', 'typename']]
},
{
model:ticketdb.subtypemaster,
attributes: [['name', 'subtypename']],
},
{
model:ticketdb.ticketsource,
attributes: ['sourcename']
}]
}]
})
问题在于,只有ticketnotifications
的子句进入实际的SQL where
子句,其余的条件tickets
和ticketdetails
进入JOIN
的地方错误。甚至Limit子句也不会生成。生成的sql如下,
SELECT count("ticketnotifications"."id") AS "count" FROM
"ticketnotifications" AS "ticketnotifications" INNER JOIN "tickets" AS
"ticket" ON "ticketnotifications"."ticketid" = "ticket"."ticketid" AND
"ticket"."isactionable" = true AND "ticket"."maintypeid" = 18 AND
"ticket"."typeid" = 70 AND "ticket"."subtypeid" = 240 INNER JOIN
"ticketdetails" AS "ticket.ticketdetail" ON "ticket"."ticketid" =
"ticket.ticketdetail"."ticketid" LEFT OUTER JOIN "maintypemaster" AS
"ticket.maintypemaster" ON "ticket"."maintypeid" =
"ticket.maintypemaster"."maintypeid" LEFT OUTER JOIN "typemaster" AS
"ticket.typemaster" ON "ticket"."typeid" =
"ticket.typemaster"."typeid" LEFT OUTER JOIN "subtypemaster" AS
"ticket.subtypemaster" ON "ticket"."subtypeid" =
"ticket.subtypemaster"."subtypeid" LEFT OUTER JOIN "ticketsource" AS
"ticket.ticketsource" ON "ticket"."sourceid" =
"ticket.ticketsource"."sourceid" WHERE "ticketnotifications"."ticketid" = 123;
我期望的是
SELECT count("ticketnotifications"."id") AS "count" FROM
"ticketnotifications" AS "ticketnotifications" INNER JOIN "tickets" AS
"ticket" ON "ticketnotifications"."ticketid" = "ticket"."ticketid" INNER JOIN
"ticketdetails" AS "ticket.ticketdetail" ON "ticket"."ticketid" =
"ticket.ticketdetail"."ticketid" LEFT OUTER JOIN "maintypemaster" AS
"ticket.maintypemaster" ON "ticket"."maintypeid" =
"ticket.maintypemaster"."maintypeid" LEFT OUTER JOIN "typemaster" AS
"ticket.typemaster" ON "ticket"."typeid" =
"ticket.typemaster"."typeid" LEFT OUTER JOIN "subtypemaster" AS
"ticket.subtypemaster" ON "ticket"."subtypeid" =
"ticket.subtypemaster"."subtypeid" LEFT OUTER JOIN "ticketsource" AS
"ticket.ticketsource" ON "ticket"."sourceid" =
"ticket.ticketsource"."sourceid" WHERE "ticketnotifications"."ticketid" = 123 AND
"ticket"."isactionable" = true AND "ticket"."maintypeid" = 18 AND
"ticket"."typeid" = 70 AND "ticket"."subtypeid" = 240
LIMIT 10 OFFSET 0;
我进行了很多搜索,得到了诸如在where
中设置$$
条件的解决方案,但这是针对一列的,我想根据过滤条件动态添加/设置where子句。
知道我在做什么错吗?
答案 0 :(得分:0)
您可以依次运营商:Operaters
因此,您的代码可以像这样:
return ticketdb.ticketnotifications.findAndCountAll({
limit: req.query.pageSize,
offset: req.query.page,
where: { [Op.and]: [{ticketId: req.query.ticketid}, {isactionable: req.query.isactionable}, {maintypeid: 18}]},
include:[{
model:ticketdb.tickets,
required: true,
where:ticketWhere,
attributes: ['isactionable'],
include:[{
model:ticketdb.ticketdetails,
required: true,
where:ticketdetailsWhere,
},
{
model:ticketdb.maintypemaster,
attributes: [['name', 'maintypename']]
},
{
model:ticketdb.typemaster,
attributes: [['name', 'typename']]
},
{
model:ticketdb.subtypemaster,
attributes: [['name', 'subtypename']],
},
{
model:ticketdb.ticketsource,
attributes: ['sourcename']
}]
}]
希望它可以为您提供帮助。 让我们根据需要对其进行修改。