node.js - Sequelize - SQL Server - 关联表的排序依据

标签 node.js express sequelize.js

我有 3 个表,例如 useruserProfileuserProfileImagesUseruserPrfoile 有很多映射,userProfileuserProfileImages 有很多映射。

我需要在 userProfileImages 中编写order by query,我试过如下,但没有成功。

User.findById(uID, { 
include: [
   model: sequelize.models.userProfile
   as: userProfile,
   include: [
    {
      model: sequelize.models.userProfileImages,
      as: 'profileImages',

    }
   ],
    order: [[sequelize.models.userProfileImages.id, "desc"]]
  // order: [["id", "desc"]] --> Tried this way also no luck
] }

我得到了结果,但是 userProfilePicture 表的结果不是 desc。

请给出解决方案

最佳答案

来自 Sequelize 官方文档:

    // Will order by an associated model's created_at using an association object. (preferred method)
    [Subtask.associations.Task, 'createdAt', 'DESC'],

    // Will order by a nested associated model's created_at using association objects. (preferred method)
    [Subtask.associations.Task, Task.associations.Project, 'createdAt', 'DESC'],

通过引用上面的语法,如下更新您的订单选项

User.findById(uID, { 
    include: [{
        model: sequelize.models.userProfile
        as: userProfile,
        include: [{
           model: sequelize.models.userProfileImages,
           as: 'profileImages',
        }],
        order: [['profileImages','id', 'desc']]
    }]
});

官方文档:http://docs.sequelizejs.com/manual/tutorial/querying.html#ordering

有关更多解决方案,请参阅此线程:https://github.com/sequelize/sequelize/issues/4553

关于node.js - Sequelize - SQL Server - 关联表的排序依据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46027310/

相关文章:

javascript - node.js + socket.io + express.js : No connection

node.js - 在 node.js 缓冲区中搜索字节模式

express - Paypal 即时更新 nvp 不工作(沙盒环境)

node.js - Node.js Express Web 服务器上的语法错误 : Unexpected identifier after installing socket. io

javascript - 如何将数据从 express.js 传递到与 html 页面相关的 Javascript 代码

node.js - 在 Sequelize 中获取另一个名称的属性?

node.js - 不能使用 dropzone 和 multer 进行多重上传

javascript - 扩展 express.Router

node.js - 如何为 Sequelize 应用程序设置应用程序名称

javascript - 无法访问 for 循环中使用的链式 promise 内的外部作用域变量