Ordering results of eager-loaded models in Node Sequelize

前端 未结 3 1673
遇见更好的自我
遇见更好的自我 2021-01-31 07:30

I have a complex set of associated models. The models are associated using join tables, each with an attribute called \'order\'. I need to be able to query the parent model \'Pa

3条回答
  •  隐瞒了意图╮
    2021-01-31 08:11

    If you also use 'as' and let's say you want to order by 'createdDate' , the query looks like this:

    DbCategoryModel.findAll({
        include: [
            {
                model: DBSubcategory,
                as: 'subcategory',
                include: [
                    {
                        model: DBProduct,
                        as: 'product',
                    }
                ],
            }
        ],
        order: [
            [
                {model: DBSubcategory, as: 'subcategory'},
                {model: DBProduct, as: 'product'},
                'createdDate',
                'DESC'
            ]
        ]
    })
    

提交回复
热议问题