2017-08-24 39 views
1

我想運行與結果排序使用主鍵,也限制了返回結果數量的查詢。例如:Sequelize MSSQL:按主鍵和限制

return Things.findAll({ attributes: [ 'id', 'status', 'otherField' ], limit: 2, order: [['id', 'DESC']] })

當查詢打造,它產生下面的SQL語句:

... ORDER BY [Source].[id] DESC, [id] OFFSET 0 ROWS FETCH NEXT 2 ROWS ONLY

因爲id是主鍵和排序參數也id我得到的以下錯誤:

'A column has been specified more than once in the order by list. Columns in the order by list must be unique.'

我使用續集3.30.4與繁瑣的2.0.0連接到Microsoft SQL服務器2017.

謝謝。

回答

0

順序數組應該包含的陣列/元組。試試這個:

return Things.findAll({ 
    attributes: [ 
    'id', 
    'status', 
    'otherField' 
    ], 
    limit: 2, 
    order: [ 
    ['id', 'DESC'] 
    ] 
}) 
+0

我的不好,我在輸入我的問題時犯了一個錯誤。我的代碼確實使用元組。我編輯了這個問題 –