Sequelize - order by in nested model not working for mysql

Viewed 122

There are 3 models in my database:

Section (1:m) Sub_Section (1:m) Questions

These 3 models have a field called order, and I want to use it to order my data.
However I am not able to use order by for this Association.

Updated the code

var SectionQuery = {
    include: [{
        model: sub_section,
        order: [['order', 'asc']],
        include: [{
            model: question,
            order: [['order', 'asc']],
        }]
    }]
};
return Section.findAll(tehsilQuery);
2 Answers
    var SectionQuery = {
        include: [{
            model: Section,
            as: "section",
            include: [{
                model: Sub_Section,
                as: "sub_section",
                include: [{
                    model: Questions,
                }]
            }]
        }],
        order: [
    [{model: Section, as: "section"}, "order", "asc"],
    [{model: Section, as: "section"}, {model: Sub_Section, as: "sub_section"}, "order", "asc"]
]
    };
    return Sections.findAll(tehsilQuery);

This should solve your issue.

Finally solved it.

var SectionQuery = {
    include: [{
        model: sub_section,
        as:"sub_sections",
        include: [{
            model: question,
            as:"questions",
        }]
    }],
    order = [
          ["order", "asc"],
          ["sub_sections","order", "asc"],
          ["sub_sections","questions","order", "asc"],
    ]
};
return Section.findAll(SectionQuery);
Related