2012-12-13 34 views
6

我有問題設置的findAll查詢與限制返回的數據順序和偏移,我使用的文檔中找到示例代碼:order: 'group DESC'但它拋出一個錯誤說::Sequelize集中返回的數據如何訂購

Error: SQLITE_ERROR: near "group": syntax error 

這裏是完整的功能。

A_Model.findAll({ 
    offset:req.query.page * req.query.rows - req.query.rows, 
    limit :req.query.rows // TODO: Of course, I put the trailing comma here ;) 
    // TODO: order: 'group DESC' 
    }) 
.success(function (docs) { 
    response_from_server.records = count; 
    response_from_server.page = req.query.page; 
    response_from_server.total = Math.ceil(count/req.query.rows); 
    response_from_server.rows = []; 

    for (item in docs) { 
     response_from_server.rows.push({ 
      id :docs[item].id, 
      cell:[ 
       docs[item].group, 
       docs[item].description, 
       docs[item].path, 
       docs[item].value 
      ] 
     }); 
    } 

    // Return the gathered data. 
    res.json(response_from_server); 
}) 
.error(function (error) {  
    logger.log('error', error); 
}); 

在此先感謝。

+0

的SQL'組by'需要通過定義一個聚合函數組。看起來你還沒有定義一個。不確定node.js是否具備執行聚合和分組的功能。 –

+1

@EricLeschinski:感謝您的評論。我最近從'mongodb'轉到'SQLite',並且對'SQL'很少了解。你能告訴我一個例子嗎? – diosney

+1

@EricLeschinski:請注意:'group'是數據庫中的一列。也許這是一個保留字? – diosney

回答

12

您給findAll方法的「order」字符串在Sequelize中沒有被解析,只能轉義以防止SQL注入。 「group」是中的保留關鍵字,大多數部分 SQL方言,所以查詢無法運行。

爲了使工作簡單地把「組」列進'的,就像這樣:

A_Model.findAll({ 
offset:req.query.page * req.query.rows - req.query.rows, 
limit :req.query.rows, 
order: '`group` DESC' 
}) 
+0

太棒了!它像一個魅力一樣工作!謝謝!!! – diosney

+1

在PostgreSQL中試過它,它應該是'order:'組DESC'',即沒有反引號。 –

+1

偉大的人像繁榮一樣尋找這項工作:0 – Adiii