2016-12-06 24 views
1

这不使用order,我不知道为什么我不能用order根表作为posts模式?当我使用这下面的代码我得到这个错误:Sequelize订单时,我们使用下面一起工作<code>Sequelize</code>罚款,我的方法

Unhandled rejection Error: 'posts' in order/group clause is not valid association 

但在其他车型如channelVideoContainer

models.posts.findAll({ 
     where: { 
      channelId: 1 
     }, 
     include: [ 
      { 
       model: models.channelVideoContainer, 
       include: [models.fileServerSetting] 
      }, { 
       model: models.channelMusicContainer, 
       include: [models.fileServerSetting] 
      }, { 
       model: models.channelImageWithTextContainer, 
       include: [models.fileServerSetting] 
      }, { 
       model: models.channelFilesContainer, 
       include: [models.fileServerSetting] 
      }, 
      models.channelPlainTextContainer 
     ], order: [ 
      [{model: models.posts}, 'id', 'DESC'], 
     ], limit: 5 
    }).then(function (result) { 
     console.log(JSON.stringify(result)); 
    }); 
+0

什么领域是你特林通过订购? 'id'?在这种情况下,我觉得应该是'命令:[“posts.id”,“DESC”]' –

+0

什么型号的关系定义是什么? – doublesharp

+0

@miparnisari我得到这个错误:'ER_BAD_FIELD_ERROR:在“为了未知列“DESC” clause'' –

回答

1

,因为要查询的posts表/模型您收到此错误做工精细,然后通过在posts模型列进行排序,但是你在你的order指定“加盟”表。这适用于其他型号,因为它们实际上已加入(使用include选件)。由于您在查询posts模型,您只需传入您想要订购的列的名称即可。请参阅文档中的一些ORDER examples

// just specify the 'id' column, 'post' is assumed because it is the queried Model 
order: [['id', 'DESC']], 

作为一个侧面说明,你可能要指定在include“d车型required: false执行LEFT JOIN让行回来,即使有在连接表不匹配。如果你知道行将被返回(或者它们实际上是必需的),那么保持原样。

{ 
    model: models.channelFilesContainer, 
    include: [models.fileServerSetting], 
    required: false, // LEFT JOIN the channelFilesContainer model 
}, 
相关问题