2017-09-03 42 views
1

我有3个表,如user,userProfileuserProfileImagesUseruserPrfoile的映射一样多,并且userProfileuserProfileImages映射的一样多。Sequelize - SQL Server - 关联表排序

我需要编写订单查询userProfileImages,我试过如下,但没有运气。

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表的结果不符合递减。

请给予解决方案

+0

请尝试使用单个数组而不是2d数组,如:[[“id”,“desc”]' – RaghavGarg

+0

我试过了,但它不起作用。还有一件事,我还在第二个包含内做了一个映射。作为 –

+0

按关键字排序也不附加在我的查询中。 –

回答

1

从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