2016-12-06 23 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 
}, 
相關問題