mysql - 在nodejs中使用关联和排序来 Sequelize findAll

标签 mysql node.js sequelize.js

我正在尝试通过以下方式让所有用户及其主供应商、预订和服务在 booking_date 之前订购。

Users.findAll({
            where: { id: userId }, attributes: AttributesUser,
            include: [{
                model: MasterVendor, attributes: AttributesMasterVendor, required: true,
                include: [{
                    model: Bookings, where: { booking_state: jobStatus, booking_date: _date }, attributes: AttributesBooking,
                    include: [{ model: Services, attributes: attributesService, order: ['Bookings.booking_date'] }],
                }],
            }],
        });

但不幸的是,order by 子句根本没有生成。但是当我尝试按名称获取数据顺序时,它工作得很好

Users.findAll({
            where: { id: userId }, attributes: AttributesUser,
            include: [{
                model: MasterVendor, attributes: AttributesMasterVendor, required: true,
                include: [{
                    model: Bookings, where: { booking_state: jobStatus, booking_date: _date }, attributes: AttributesBooking,
                    include: { model: Services, attributes: attributesService }
                }]
            }],
            order: ['name'],
        });

请建议我,我做错了什么。 谢谢。

最佳答案

试试这个

在订单数组中包含型号名称。

Users.findAll({
            where: { id: userId }, attributes: AttributesUser,
            include: [{
                model: MasterVendor, attributes: AttributesMasterVendor, required: true,
                include: [{
                    model: Bookings, where: { booking_state: jobStatus, booking_date: _date }, attributes: AttributesBooking,
                    include: { model: Services, attributes: attributesService }
                }]
            }],
            order: [[Bookings, 'booking_date', 'DESC']],
        });

关于mysql - 在nodejs中使用关联和排序来 Sequelize findAll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55109136/

相关文章:

MySQL触发变量排序时出错

php - 如何用 PHP 和 MYSQL 分离页面?

node.js - express 无法验证

javascript - 在事件处理程序中读写全局变量 : when is concurrency an issue ?

javascript - SequelizebelongsToMany 关联不起作用

php - 如何将动态数据库值从一个html元素连接到另一个?

java - 来自服务器的错误 : Data source rejected establishment of connection, 消息: "Too many connections"

javascript - If 子句括号和相等的优先级

mysql - 如何在 MySQL 或 Sequelize 中禁用 only_full_group_by

mysql - 按计数关联排序?