Feathers 和 Sequelize SQL 服务器错误
Feathers and Sequelize SQL Server Errors
我 运行 遇到几个 sql 服务器错误,试图开始使用 feathers js 和 sequelize 进行简单的休息 api。我正在使用 SQL Server 2012 (express) 版本。
首先是我的代码:
const { db } = require('../database');
const Sequelize = require('sequelize');
const service = require('feathers-sequelize');
const app = require('../app');
const Model = db.define('workorder', {
id: { type: Sequelize.INTEGER, primaryKey: true },
created_by: Sequelize.STRING,
modified_by: Sequelize.STRING,
status_name: Sequelize.STRING,
date_completed: Sequelize.DATE,
category: Sequelize.STRING,
location: Sequelize.STRING,
details: Sequelize.TEXT,
crew_name: Sequelize.STRING,
assigned_to: Sequelize.STRING,
}, {
schema: 'dbo',
freezeTableName: true,
createdAt: 'date_created',
updatedAt: 'date_modified',
});
app.use('/api/workorders', service({
Model,
id: 'id',
paginate: {
default: 10,
max: 100,
},
}));
当我点击 api 端点时 /api/workorders
它出错了:
SequelizeDatabaseError:FETCH 语句中选项 NEXT 的使用无效。
我看到 sql 生成了:
Executing (default): SELECT [id], [created_by], [modified_by], [status_name], [date_completed], [category], [location], [details], [crew_name], [assigned_to], [date_created], [date_modified] FROM [dbo].[workorder] AS [workorder] OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY;
似乎 sql 服务器需要一些 ORDER BY 子句。所以我尝试将参数发送到端点 /api/workorders?$sort=id
这也出错了:
SequelizeDatabaseError: 列名称“1”无效。
生成的SQL看起来像这样:
Executing (default): SELECT [id], [created_by], [modified_by], [status_name], [date_completed], [category], [location], [details], [crew_name], [assigned_to], [date_created], [date_modified] FROM [dbo].[workorder] AS [workorder] ORDER BY [workorder].[0] DESC, [workorder].[1] DESC OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY;
这个错误比较明显,没有第1列或第0列。
有没有人运行以前遇到过这些问题?
这似乎是 Sequelize 与旧版本的 MSSQL 服务器不兼容,如 this issue. A Feathers specific solution mentioned in this issue comment 中提到的那样添加 $sort
和 $limit
:
query: {
$limit: 5,
$sort: {createdAt: -1}
}
正如 Daff 所提到的,我也发现对于 SQL Server 2014,Sequelize 仍然没有生成 MSSQL 2014 兼容代码。因此,鉴于建议 here 指定 MSSQL 低于 11.0.0 的版本,我指定了有点武断的数据库版本 10.0.0。
这对我有用 - Sequelize 现在生成 2014(及更早版本)兼容的 tsql 代码。我在从 feathers-cli 生成的 'sequelize.js' 文件中生成 Sequelize 实例时,对传递的选项对象中的 databaseVersion 属性 进行了更改,如下所示:
const sequelize = new Sequelize(database, username, password, {
dialect: 'mssql',
host: hostname,
logging: console.log,
define: {
freezeTableName: true
},
// from https://github.com/sequelize/sequelize/issues/4404
// use earlier SQL Server version to assure tsql compatible code generation
databaseVersion: '10.0.0'
});
我 运行 遇到几个 sql 服务器错误,试图开始使用 feathers js 和 sequelize 进行简单的休息 api。我正在使用 SQL Server 2012 (express) 版本。
首先是我的代码:
const { db } = require('../database');
const Sequelize = require('sequelize');
const service = require('feathers-sequelize');
const app = require('../app');
const Model = db.define('workorder', {
id: { type: Sequelize.INTEGER, primaryKey: true },
created_by: Sequelize.STRING,
modified_by: Sequelize.STRING,
status_name: Sequelize.STRING,
date_completed: Sequelize.DATE,
category: Sequelize.STRING,
location: Sequelize.STRING,
details: Sequelize.TEXT,
crew_name: Sequelize.STRING,
assigned_to: Sequelize.STRING,
}, {
schema: 'dbo',
freezeTableName: true,
createdAt: 'date_created',
updatedAt: 'date_modified',
});
app.use('/api/workorders', service({
Model,
id: 'id',
paginate: {
default: 10,
max: 100,
},
}));
当我点击 api 端点时 /api/workorders
它出错了:
SequelizeDatabaseError:FETCH 语句中选项 NEXT 的使用无效。
我看到 sql 生成了:
Executing (default): SELECT [id], [created_by], [modified_by], [status_name], [date_completed], [category], [location], [details], [crew_name], [assigned_to], [date_created], [date_modified] FROM [dbo].[workorder] AS [workorder] OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY;
似乎 sql 服务器需要一些 ORDER BY 子句。所以我尝试将参数发送到端点 /api/workorders?$sort=id
这也出错了:
SequelizeDatabaseError: 列名称“1”无效。
生成的SQL看起来像这样:
Executing (default): SELECT [id], [created_by], [modified_by], [status_name], [date_completed], [category], [location], [details], [crew_name], [assigned_to], [date_created], [date_modified] FROM [dbo].[workorder] AS [workorder] ORDER BY [workorder].[0] DESC, [workorder].[1] DESC OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY;
这个错误比较明显,没有第1列或第0列。
有没有人运行以前遇到过这些问题?
这似乎是 Sequelize 与旧版本的 MSSQL 服务器不兼容,如 this issue. A Feathers specific solution mentioned in this issue comment 中提到的那样添加 $sort
和 $limit
:
query: {
$limit: 5,
$sort: {createdAt: -1}
}
正如 Daff 所提到的,我也发现对于 SQL Server 2014,Sequelize 仍然没有生成 MSSQL 2014 兼容代码。因此,鉴于建议 here 指定 MSSQL 低于 11.0.0 的版本,我指定了有点武断的数据库版本 10.0.0。
这对我有用 - Sequelize 现在生成 2014(及更早版本)兼容的 tsql 代码。我在从 feathers-cli 生成的 'sequelize.js' 文件中生成 Sequelize 实例时,对传递的选项对象中的 databaseVersion 属性 进行了更改,如下所示:
const sequelize = new Sequelize(database, username, password, {
dialect: 'mssql',
host: hostname,
logging: console.log,
define: {
freezeTableName: true
},
// from https://github.com/sequelize/sequelize/issues/4404
// use earlier SQL Server version to assure tsql compatible code generation
databaseVersion: '10.0.0'
});