Tedious 或 Sequelize 对 `findOne()` 使用了错误的语法 [英] Tedious or Sequelize uses the wrong syntax for `findOne()`

查看:11
本文介绍了Tedious 或 Sequelize 对 `findOne()` 使用了错误的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Sequelize with Tedious 来访问 SQL Server 2008.

I am using Sequelize with Tedious to access SQL Server 2008.

当我执行 sequelizeModel.findOne() 时,我得到了这个异常 -

When I do a sequelizeModel.findOne() I get this exception -

未处理的拒绝 SequelizeDatabaseError:FETCH 语句中选项 NEXT 的使用无效.

Unhandled rejection SequelizeDatabaseError: Invalid usage of the option NEXT in the FETCH statement.

我知道 SQL Server 2008 不支持 OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY,这就是引发异常的原因.

I know SQL Server 2008 doesn't support OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY and that is why the exception is thrown.

但我也将繁琐选项中的tdsVersion明确设置为7_3_B.

But I have also explicitly set the tdsVersion in the tedious options to 7_3_B.

如此处所述 -
http://pekim.github.io/tedious/api-connection.html

我已经尝试了所有的 tds 版本,并且生成的查询语法总是包含 FETCH/NEXT 语法.

I've tried all the tds versions and the query syntax that is generated always contains the FETCH/NEXT syntax.

我错过了什么吗?

语法不应该特定于 tds 版本吗?

Shouldn't the syntax be specific to the tds version?

我还验证了 tdsVersion 选项已成功从 sequelize 传递到繁琐的连接库.

I've also verified that the tdsVersion option is being passed successfully to the tedious connection library from sequelize.

生成的查询语法示例 -

Example of query syntax generated -

SELECT 
    [id], [FIRST_NAME], [LAST_NAME]  
FROM  
    [USERs] AS [USERS] 
ORDER BY 
    [id]  
    OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY;

推荐答案

这是 Sequelize 中的一个问题 -- 它使用 OFFSET FETCH 语法,仅在 SQL Server 2012 及更高版本中受支持.

This is an issue in Sequelize -- it uses the OFFSET FETCH syntax, which is only supported in SQL Server 2012 and newer.

我在 GitHub 上将此作为问题提交:https://github.com/sequelize/sequelize/issues/4404

I submitted this as an issue on GitHub: https://github.com/sequelize/sequelize/issues/4404

该问题还会影响 findById 方法.该方法的解决方法是使用 findAllwhere 来指定 ID,并且只使用返回数组中的第一个元素:

The issue also affects the findById method. A workaround for that method is to use findAll with a where to specify the ID, and just only use the first element from the returned array:

Thing.findAll({
  where: {id: id}
}).then( function(things) {
  if (things.length == 0) {
    // handle error
  }
  doSomething(things[0])
}).catch( function(err) {
  // handle error
});

这篇关于Tedious 或 Sequelize 对 `findOne()` 使用了错误的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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