Sequelize - SQL Server - order by for association tables

前端 未结 2 881
后悔当初
后悔当初 2021-01-02 18:19

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

相关标签:
2条回答
  • 2021-01-02 18:40

    Update your order in association like below

    User.findById(uID, { 
    include: [
        model: sequelize.models.userProfile,
        as: userProfile,        
        include: [{
           model: sequelize.models.userProfileImages,
           as: 'profileImages',
           separate:true
           order: [['id', 'desc']]
        }],
    ]});
    
    0 讨论(0)
  • 2021-01-02 18:49

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

    Official Documentations: http://docs.sequelizejs.com/manual/tutorial/querying.html#ordering

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

    0 讨论(0)
提交回复
热议问题