до сих пор работал
model.specialistServiceCategory
.findAll({
where: { specialistId: req.query.loggedInUserData.userId }
})
.then(d => {
let sServiceCatArr = d.map(i => i.categoryId);
var searchCond = {
where: { status: "OPEN", categoryId: sServiceCatArr, id: { [Op.notIn]: [sequelize.literal("SELECT `bidId` FROM `bid_response` WHERE `specialistId` =" + req.query.loggedInUserData.userId)] } },
};
model.bid
.count(searchCond)
.then(totalRecord => {
var totalPage = Math.ceil(totalRecord / limit);
model.bid
.findAll({
where: searchCond.where,
include: [
{
model: model.requester, attributes: ["id", "firstName", "lastName", "email", "mobile",
[sequelize.fn("if", sequelize.literal("profileImage IS NOT NULL"), sequelize.fn("concat", req.app.get("httpUrlUpload"), sequelize.col("profileImage")), ""), "profileImage"]
], required: false
}, {model:model.bidResponse, attributes:{
include: ['bidId']
}, include: {model:model.reviewRating, attributes: [[model.sequelize.fn('AVG', model.sequelize.col('rating')),'avgRating']] } }
/*{
model: model.bidResponse,
where: { specialistId: req.query.loggedInUserData.userId },
required: false
}*/
],
limit: limit,
offset: offset,
$sort: { id: 1 },
group: [model.sequelize.literal('`id`')]
})
.then(data => util.sendPaginateResponse(res, data, totalRecord, totalPage, page))
.catch(e => util.handleError(e, res));
})
.catch(e => util.handleError(e, res));
})
.catch(e => util.handleError(e, res));
Запрос, сгенерированный через выше Sequelize ORM
SELECT `bid`.*, `requester`.`id` AS `requester.id`, `requester`.`firstName` AS `requester.firstName`, `requester`.`lastName` AS `requester.lastName`, `requester`.`email` AS `requester.email`, `requester`.`mobile` AS `requester.mobile`, if(profileImage IS NOT NULL, concat('http://localhost:3002/uploads/', `profileImage`), '') AS `requester.profileImage`, `bidResponses`.`id` AS `bidResponses.id`, `bidResponses`.`bidId` AS `bidResponses.bidId`, `bidResponses`.`specialistId` AS `bidResponses.specialistId`, `bidResponses`.`price` AS `bidResponses.price`, `bidResponses`.`createdAt` AS `bidResponses.createdAt`, `bidResponses`.`updatedAt` AS `bidResponses.updatedAt`, `bidResponses`.`bidId` AS `bidResponses.bidId`, `bidResponses->reviewRatings`.`id` AS `bidResponses.reviewRatings.id`, AVG(`rating`) AS `bidResponses.reviewRatings.avgRating` FROM (SELECT `bid`.`id`, `bid`.`requesterId`, `bid`.`categoryId`, `bid`.`status`, `bid`.`scheduleDate`, `bid`.`expiryDateTime`, `bid`.`description`, `bid`.`latitude`, `bid`.`longitude`, `bid`.`createdAt`, `bid`.`updatedAt` FROM `bid` AS `bid` WHERE `bid`.`status` = 'OPEN' AND `bid`.`categoryId` IN (1, 3, 4) AND `bid`.`id` NOT IN (SELECT `bidId` FROM `bid_response` WHERE `specialistId` =6) GROUP BY `id` LIMIT 0, 10) AS `bid` LEFT OUTER JOIN `requester` AS `requester` ON `bid`.`requesterId` = `requester`.`id` LEFT OUTER JOIN `bid_response` AS `bidResponses` ON `bid`.`id` = `bidResponses`.`bidId` LEFT OUTER JOIN `review_rating` AS `bidResponses->reviewRatings` ON `bidResponses`.`specialistId` = `bidResponses->reviewRatings`.`referenceId`;
Обязательный запрос
SELECT `bid`.*, `requester`.`id` AS `requester.id`, `requester`.`firstName` AS `requester.firstName`, `requester`.`lastName` AS `requester.lastName`, `requester`.`email` AS `requester.email`, `requester`.`mobile` AS `requester.mobile`, if(profileImage IS NOT NULL, concat('http://localhost:3002/uploads/', `profileImage`), '') AS `requester.profileImage`, `bidResponses`.`id` AS `bidResponses.id`, `bidResponses`.`bidId` AS `bidResponses.bidId`, `bidResponses`.`specialistId` AS `bidResponses.specialistId`, `bidResponses`.`price` AS `bidResponses.price`, `bidResponses`.`createdAt` AS `bidResponses.createdAt`, `bidResponses`.`updatedAt` AS `bidResponses.updatedAt`, `bidResponses`.`bidId` AS `bidResponses.bidId`, `bidResponses->reviewRatings`.`id` AS `bidResponses.reviewRatings.id`, AVG(`rating`) AS `bidResponses.reviewRatings.avgRating` FROM (SELECT `bid`.`id`, `bid`.`requesterId`, `bid`.`categoryId`, `bid`.`status`, `bid`.`scheduleDate`, `bid`.`expiryDateTime`, `bid`.`description`, `bid`.`latitude`, `bid`.`longitude`, `bid`.`createdAt`, `bid`.`updatedAt` FROM `bid` AS `bid` WHERE `bid`.`status` = 'OPEN' AND `bid`.`categoryId` IN (1, 3, 4) AND `bid`.`id` NOT IN (SELECT `bidId` FROM `bid_response` WHERE `specialistId` =6) LIMIT 0, 10) AS `bid` LEFT OUTER JOIN `requester` AS `requester` ON `bid`.`requesterId` = `requester`.`id` LEFT OUTER JOIN `bid_response` AS `bidResponses` ON `bid`.`id` = `bidResponses`.`bidId` LEFT OUTER JOIN `review_rating` AS `bidResponses->reviewRatings` ON `bidResponses`.`specialistId` = `bidResponses->reviewRatings`.`referenceId` GROUP BY `id`;
, как вы можете видеть небольшую разницу вВышеупомянутый запрос в обязательном запросе, группа by находится в последнем, который представляет правильные данные, в то время как в неправильном запросе, который генерируется через sequelize.js, а узел имеет группу в подзапросе.