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

查看:285
本文介绍了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中写出按查询顺序,我尝试了如下操作,但是没有运气.

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表的结果却不尽如人意.

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/问题/4553

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

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

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