Sequelize - SQL Server - 关联表的排序 [英] Sequelize - SQL Server - order by for association tables

查看:11
本文介绍了Sequelize - SQL Server - 关联表的排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个表,例如 useruserProfileuserProfileImages.UseruserPrfoile 的映射有很多,userProfileuserProfileImages 的映射有很多.

I have 3 tables such as user, userProfile and userProfileImages. User is mapping with userPrfoile as has many and userProfile is mapping with userProfileImages as has many.

我需要在 userProfileImages 中编写 order by query,我尝试如下,但没有运气.

I need to write the order by query in userProfileImages, I tried as below, but no luck.

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 不一样.

I am getting the result, but userProfilePicture table's result is not as desc.

请给出解决方案

推荐答案

来自 Sequelize 官方文档:

From Sequelize offical docs:

    // 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'],

通过参考上述语法,更新您的订单选项,如下所示

By referring above syntax, Update your order option like below

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

Refer this thread for more solutions: https://github.com/sequelize/sequelize/issues/4553

这篇关于Sequelize - SQL Server - 关联表的排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆