Sequelize mssql:按主键和限制排序 [英] Sequelize mssql: order by primary key and limit

查看:40
本文介绍了Sequelize mssql:按主键和限制排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用主键运行结果排序的查询,并限制返回结果的数量.例如:

I want to run a query with results sort using the primary key and also limit the number of return results. For example:

return Things.findAll({属性: ['ID','地位','其他字段'],限制:2,顺序:[['id', 'DESC']]})

当查询被构建时,它会生成以下 SQL 语句:

when the query is build, it generate the following SQL statement:

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

因为 id 是主键并且排序参数也是 id 我收到以下错误:

Because id is the primary key and the sort parameter is also id I get the following error:

'在 order by 列表中多次指定一列.order by 列表中的列必须是唯一的.'

我使用 sequelize 3.30.4 和乏味的 2.0.0 连接到 Microsoft SQL Server 2017.

I am using sequelize 3.30.4 with tedious 2.0.0 connecting to a Microsoft SQL server 2017.

谢谢.

推荐答案

默认情况下,Sequelize 在每个表中创建 createdAt 列.所以你可以通过这样做来处理这个问题:

By the default, Sequelize create createdAt column in each table. So you can handle this issue by doing like this:

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

createdAt列为参数进行排序,排序结果与id列相同

Do the sorting by using column createdAt as the param, the sorting result have same result as you do with id column

这篇关于Sequelize mssql:按主键和限制排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆