Sequelize:在应用 imit 之前排序

发布于 2025-01-09 02:04:30 字数 3345 浏览 0 评论 0原文

我的查询是在设置限制后对数组进行排序,因此数组未正确排序,从而返回错误的数据。我想按关联对象的日期属性对数组进行排序,然后限制数组并使用偏移量。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文