2012-12-13 31 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