Sequelize:在应用 imit 之前排序
我的查询是在设置限制后对数组进行排序,因此数组未正确排序,从而返回错误的数据。我想按关联对象的日期属性对数组进行排序,然后限制数组并使用偏移量。
await dbBooking.findAll({
include: [
{
model: dbEvent,
attributes: defaultEventsAttributes,
include: [
{
model: dbUser,
through: { attributes: [] },
attributes: defaultUsersAttributes,
},
],
},
],
order: [['event', 'startTime', 'ASC']],
offset: offset,
limit: 6,
attributes: defaultBookingsAttributes,
});
SQL 查询
SELECT `booking`.*,
`event`.`id` AS `event.id`,
`event`.`title` AS `event.title`,
`event`.`description` AS `event.description`,
`event`.`starttime` AS `event.startTime`,
`event`.`endtime` AS `event.endTime`,
`event->users`.`id` AS `event.users.id`,
`event->users`.`email` AS `event.users.email`
FROM (SELECT `booking`.`id`,
`booking`.`totalprice`,
`booking`.`eventid`
FROM `bookings` AS `booking`
LIMIT 0, 6) AS `booking`
LEFT OUTER JOIN `events` AS `event`
ON `booking`.`eventid` = `event`.`id`
LEFT OUTER JOIN ( `eventusers` AS `event->users->eventuser`
INNER JOIN `users` AS `event->users`
ON `event->users`.`id` =
`event->users->eventuser`.`userid`)
ON `event`.`id` = `event->users->eventuser`.`eventid`
ORDER BY `event`.`starttime` ASC;
没有 subQuery:false subQuery: false
SELECT `booking`.`id`,
`booking`.`totalprice`,
`event`.`id` AS `event.id`,
`event`.`title` AS `event.title`,
`event`.`description` AS `event.description`,
`event`.`starttime` AS `event.startTime`,
`event`.`endtime` AS `event.endTime`,
`event->users`.`id` AS `event.users.id`,
`event->users`.`email` AS `event.users.email`
FROM `bookings` AS `booking`
LEFT OUTER JOIN `events` AS `event`
ON `booking`.`eventid` = `event`.`id`
LEFT OUTER JOIN ( `eventusers` AS `event->users->eventuser`
INNER JOIN `users` AS `event->users`
ON `event->users`.`id` =
`event->users->eventuser`.`userid`)
ON `event`.`id` = `event->users->eventuser`.`eventid`
ORDER BY `event`.`starttime` ASC
LIMIT 0, 6;
关联的
Booking.belongsTo(User);
Booking.belongsTo(Event);
User.belongsToMany(Event, {
through: { model: EventUsers, unique: false },
foreignKey: 'userId',
});
Event.belongsToMany(User, {
through: { model: EventUsers, unique: false },
foreignKey: 'eventId',
});
My query is sorting the array after the limit has been set, so the array is not sorted properly thus returning wrong data. I want to sort the array by associated object's date attribute, and after that, limit the array and use the offset.
await dbBooking.findAll({
include: [
{
model: dbEvent,
attributes: defaultEventsAttributes,
include: [
{
model: dbUser,
through: { attributes: [] },
attributes: defaultUsersAttributes,
},
],
},
],
order: [['event', 'startTime', 'ASC']],
offset: offset,
limit: 6,
attributes: defaultBookingsAttributes,
});
The SQL Query without subQuery:false
SELECT `booking`.*,
`event`.`id` AS `event.id`,
`event`.`title` AS `event.title`,
`event`.`description` AS `event.description`,
`event`.`starttime` AS `event.startTime`,
`event`.`endtime` AS `event.endTime`,
`event->users`.`id` AS `event.users.id`,
`event->users`.`email` AS `event.users.email`
FROM (SELECT `booking`.`id`,
`booking`.`totalprice`,
`booking`.`eventid`
FROM `bookings` AS `booking`
LIMIT 0, 6) AS `booking`
LEFT OUTER JOIN `events` AS `event`
ON `booking`.`eventid` = `event`.`id`
LEFT OUTER JOIN ( `eventusers` AS `event->users->eventuser`
INNER JOIN `users` AS `event->users`
ON `event->users`.`id` =
`event->users->eventuser`.`userid`)
ON `event`.`id` = `event->users->eventuser`.`eventid`
ORDER BY `event`.`starttime` ASC;
subQuery: false
SELECT `booking`.`id`,
`booking`.`totalprice`,
`event`.`id` AS `event.id`,
`event`.`title` AS `event.title`,
`event`.`description` AS `event.description`,
`event`.`starttime` AS `event.startTime`,
`event`.`endtime` AS `event.endTime`,
`event->users`.`id` AS `event.users.id`,
`event->users`.`email` AS `event.users.email`
FROM `bookings` AS `booking`
LEFT OUTER JOIN `events` AS `event`
ON `booking`.`eventid` = `event`.`id`
LEFT OUTER JOIN ( `eventusers` AS `event->users->eventuser`
INNER JOIN `users` AS `event->users`
ON `event->users`.`id` =
`event->users->eventuser`.`userid`)
ON `event`.`id` = `event->users->eventuser`.`eventid`
ORDER BY `event`.`starttime` ASC
LIMIT 0, 6;
associations
Booking.belongsTo(User);
Booking.belongsTo(Event);
User.belongsToMany(Event, {
through: { model: EventUsers, unique: false },
foreignKey: 'userId',
});
Event.belongsToMany(User, {
through: { model: EventUsers, unique: false },
foreignKey: 'eventId',
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论