【问题标题】:sequelize findAll with association and ordering in nodejs用nodejs中的关联和排序对findAll进行续集
【发布时间】:2019-03-12 21:13:11
【问题描述】:

我正在尝试通过以下方式在 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'],
        });

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

【问题讨论】:

    标签: mysql node.js sequelize.js


    【解决方案1】:

    试试这个

    在顺序数组中包含模型名称。

    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']],
            });
    

    【讨论】:

      【解决方案2】:

      最后,我解决了这个问题。我必须在顺序数组中的子表(预订)之前添加父表(MasterVendor)

      order: [[MasterVendor,Bookings, 'booking_date', 'ASC']],
      

      最后,

      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: [[MasterVendor, Bookings, 'booking_date', 'ASC']],
              });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-11
        • 2016-07-15
        • 1970-01-01
        • 1970-01-01
        • 2014-06-30
        • 2018-06-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多