在sequelize无法正常工作的嵌套eager loading中排序

时间:2016-10-23 11:47:17

标签: node.js sequelize.js eager-loading

我有四个模特Tehsil,Ilr,Patwar和Villages。 和他们的关系是

Tehsil - > 1:m - > Ilr - > 1:m - > Patwar - > 1:m - >村庄

我想在我的所有四个模型上应用订单。

查询:

var tehsilQuery = {
    include: [{
        model: Ilr,
        as: 'GirdawariKanoongo',
        include: [{
            model: Patwar,
            as: 'GirdawariPatwar',
            include: [{
                model: Villages,
                as: 'GirdawariVillages',
            }]
        }]
    }],
    order: [
        ['tehsil_name', 'ASC'],
        [ {model: Ilr, as: 'GirdawariKanoongo'}, 'kanoongo_name', 'ASC'],
        [ {model: Patwar, as: 'GirdawariPatwar'}, 'patwar_area', 'ASC'],
        [ {model: Villages, as: 'GirdawariVillages'}, 'village_name', 'ASC'],
    ]
};
return Tehsils.findAll(tehsilQuery);

[Error: 'girdawari_patwar' in order / group clause is not valid association]

如果我从Patwar删除Villagesorder(lat两个模型),则按顺序工作。

4 个答案:

答案 0 :(得分:3)

另一个嵌套排序的工作示例:

order: [  
  [ { model: chapterModel, as: 'Chapters' }, 'createdAt', 'ASC'], 
  [ { model: chapterModel, as: 'Chapters' }, 
    { model: partModel, as: 'Parts' }, 'createdAt', 'ASC'] 
],

其中part和chapter具有M:1关系。

答案 1 :(得分:2)

我解决了。 这是order看起来的样子:

order: [
        'tehsil_name',
        'GirdawariKanoongo.kanoongo_name',
        'GirdawariKanoongo.GirdawariPatwar.patwar_area',
        'GirdawariKanoongo.GirdawariPatwar.GirdawariVillages.village_name' 
       ]

我所要做的就是:在表格的关联中使用as并将它们链接起来,然后column_name应该适用order

答案 2 :(得分:0)

我们的场景是使用两个嵌套的List<String> lines = Files.readAllLines(pathToFile); 语句,其中最里面的嵌套没有正确排序。通过在include的最高级别应用ordering,我们能够成功返回有序对象。

我们的findAll的模型关系如下:

  • suveySet有很多调查
  • 调查属于很多问题

    surveySet.findAll

答案 3 :(得分:0)

对于任何想知道如何使用对象样式定义来实现的人,这就是解决方法。 您可以在Sequelize documentation中找到有关订购的更多信息。

order: [
    ['tehsil_name', 'ASC'],
    [ {model: Ilr, as: 'GirdawariKanoongo'}, 'kanoongo_name', 'ASC'],
    [ {model: Ilr, as: 'GirdawariKanoongo'}, {model: Patwar, as: 'GirdawariPatwar'}, 'patwar_area', 'ASC'],
    [ {model: Ilr, as: 'GirdawariKanoongo'}, {model: Patwar, as: 'GirdawariPatwar'}, {model: Villages, as: 'GirdawariVillages'}, 'village_name', 'ASC'],
]