Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

:Sequelize how set returned data order

I've issues set the returned data order of a findAll query with limit and offset, i'm using example code found in the documentation: order: 'group DESC' but it throw an error saying:

Error: SQLITE_ERROR: near "group": syntax error

Here is the complete function.

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);
});

Thanks in advance.

like image 245
Diosney Avatar asked Dec 13 '12 16:12

Diosney


1 Answers

The "order"-string you give to the findAll method is not parsed in Sequelize, only escaped to prevent SQL-injections. "group" is a reserved keyword in most some SQL-dialects, so the query fails to run.

To make it work simply put the "group" column into `'s, like this:

A_Model.findAll({
 offset:req.query.page * req.query.rows - req.query.rows,
 limit :req.query.rows,
 order: '`group` DESC'
})
like image 56
tomahaug Avatar answered Sep 17 '22 00:09

tomahaug