羽毛和Sequelize SQL Server错误

时间:2018-01-10 22:16:37

标签: sql-server sequelize.js feathersjs feathers-sequelize

我遇到了几个sql server错误,尝试使用羽毛js和sequelize开始使用简单的rest api。我使用的是SQL Server 2012(快速)版本。

首先是我的代码:

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 server需要一些ORDER BY子句。所以我尝试将一个参数发送到端点/api/workorders?$sort=id

这也是错误的:

SequelizeDatabaseError:列名称无效' 1'。

SQL Generated看起来像这样:

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列。

之前有人遇到过这些问题吗?

2 个答案:

答案 0 :(得分:1)

这似乎是Sequelize与this issue中提到的旧版MSSQL服务器不兼容。提及in this issue comment的Feathers特定解决方案可添加$sort$limit,如下所示:

query: {
        $limit: 5,
        $sort: {createdAt: -1}
      }

答案 1 :(得分:1)

正如Daff所提到的,我也发现对于SQL Server 2014,Sequelize仍然没有生成MSSQL 2014兼容代码。因此,鉴于建议here指定的MSSQL版本低于11.0.0,我在某种程度上任意指定了10.0.0的数据库版本。

这对我有用 - Sequelize现在生成2014(及更早版本)兼容的tsql代码。我在使用feathers-cli生成的'sequelize.js'文件中生成Sequelize实例时传递的options对象中对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' 
});