Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use order inside include in sequelize

Discussion.findAll({
    where : {
        discussion_on : profileId,
        is_deleted : false ,
        is_reply : false
    },
    limit : 5,
    order : [['updated_at','DESC']],
    offset:5*(page-1),
    // order : [['updated_at','DESC']],

    include:[{
        model : Profile,
        as : 'CommentBy',
        attributes:staticData.USER_LAYOUT,
        include : [{
            model : TeamMember,
            as : "TeamSender",
            required : false,
            attributes : ['receiver', 'title'],
            where : sequelize.where(sequelize.col('receiver'),db.sequelize.col('discussion.comment_entity'))
        },{
            model : TeamMember,
            required : false,
            as : "TeamReceiver",
            attributes : ['sender', 'title'],
            where : sequelize.where(sequelize.col('CommentBy.TeamReceiver.sender'),db.sequelize.col('discussion.comment_entity'))
        }]
    },{
        model : Discussion,
        as : 'Replies',
        required : false ,
        order : [['Replies.created_at','ASC']],
        where : {
            is_deleted : false
        } ,
        include:[{
            model : Profile,
            as : "CommentBy",
            attributes:staticData.USER_LAYOUT,
            include : [{
                model : TeamMember,
                as : "TeamSender",
                required : false,
                attributes : ['receiver', 'title'],
                where : sequelize.where(sequelize.col('Replies.CommentBy.TeamSender.receiver'),db.sequelize.col('Replies.comment_entity'))
            },{
                model : TeamMember,
                required : false,
                as : "TeamReceiver",
                attributes : ['sender', 'title'],
                where : sequelize.where(sequelize.col('Replies.CommentBy.TeamReceiver.sender'),db.sequelize.col('Replies.comment_entity'))
            }]
        },{
            model:Profile,
            as : "CommentEntity",
            attributes:staticData.USER_LAYOUT
        }]
    },{
            model:Profile,
            as : "CommentEntity",
            attributes:staticData.USER_LAYOUT
    }]
})

Discussion is the object name. Basically this query fetches all the comments with their replies attached to each comment .I want the replies to be sorted by date as well.

This is what i want to achieve inside Replies . But it does not work

order : [['created_at','ASC']],

like image 911
Yash Sharma Avatar asked Aug 13 '26 20:08

Yash Sharma


1 Answers

Try this, It will helpful for you.

Discussion.findAll({
limit: 5,
....
order: [
        ['updated_at', 'DESC']
        ['Replies','created_at', 'ASC'],
       
    ],
....
})

You have to define like this

like image 101
ovesh kazi Avatar answered Aug 16 '26 11:08

ovesh kazi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!