如果字符串为"id"以数字开头,则 GET 请求在字符串为"id"的模型上失败 - Trailsjs



我有一个名为Product的模型,它有两个字段id和name。我安装了脚印。

'use strict'
const Model = require('trails/model')
/**
* @module Product
* @description TODO document Model
*/
module.exports = class Productextends Model {
static config (app, Sequelize) {
return {
store: 'db',
options: {
schema: 'dbo',
tableName: 'dimProduct',
timestamps: false,
classMethods: {
//If you need associations, put them here
associate: (models) => {
}
}
}
}
}
static schema (app, Sequelize) {
return {
id: {
type: Sequelize.STRING,
allowNull: false,
primaryKey: true,
field: 'ProductCode'
},
name: {
type: Sequelize.STRING,
field: 'ProductName'
}
}
}
}

当我使用poster和GET请求localhost:3000/product?id=XX2525时,记录器中生成的sql是

SELECT [Product].[ProductCode] AS [id], [Product].[ProductName] AS [name] FROM [dbo].[dimProduct] AS [Product] WHERE [Product].[ProductCode] = N'XX2525';

如果我对一个以数字localhost:3000/product?id=10XX2525开头的id执行相同的查询,则生成的sql是

SELECT [Product].[ProductCode] AS [id], [Product].[ProductName] AS [name] FROM [dbo].[dimProduct] AS [Product] WHERE [Product].[ProductCode] = 10;

我不确定这是trails还是sequelize,但如果我在模型中将字段定义为字符串,我希望查询以字符串的形式搜索,而不应用任何转换。错误看起来像(001CAR是我数据库中的第一个id):

{
"name": "SequelizeDatabaseError",
"message": "Conversion failed when converting the varchar value '1CAR' to data type int.",
"parent": {
"message": "Conversion failed when converting the varchar value '1CAR' to data type int.",
"code": "EREQUEST",
"number": 245,
"state": 1,
"class": 16,
"serverName": "db",
"procName": "",
"lineNumber": 1,
"sql": "SELECT [Product].[ProductCode] AS [id], [Product].[ProductName] AS [name] FROM [dbo].[dimProduct] AS [Product] WHERE [Product].[ProductCode] = 10;"
},
"original": {
"message": "Conversion failed when converting the varchar value '1CAR' to data type int.",
"code": "EREQUEST",
"number": 245,
"state": 1,
"class": 16,
"serverName": "db",
"procName": "",
"lineNumber": 1,
"sql": "SELECT [Product].[ProductCode] AS [id], [Product].[ProductName] AS [name] FROM [dbo].[dimProduct] AS [Product] WHERE [Product].[ProductCode] = 10;"
},
"sql": "SELECT [Product].[ProductCode] AS [id], [Product].[ProductName] AS [name] FROM [dbo].[dimProduct] AS [Product] WHERE [Product].[ProductCode] = 10;",
"isBoom": true,
"isServer": true,
"data": null,
"output": {
"statusCode": 500,
"payload": {
"statusCode": 500,
"error": "Internal Server Error",
"message": "An internal server error occurred"
},
"headers": {}
}
}

我的package.json依赖项是,并且我正在运行nodev6.11.5

"dependencies": {
"ejs": "^2.5.7",
"express": "^4.16.2",
"tedious": "^2.0.0",
"trailpack-express": "^2.0.3",
"trailpack-footprints": "^2.0.0",
"trailpack-repl": "v2-latest",
"trailpack-router": "v2-latest",
"trailpack-sequelize": "^2.0.0",
"trails": "v2-latest"
}

您遇到了一个已经由该PR修复的错误https://github.com/trailsjs/trailpack/pull/44但该修复程序显然没有部署在npm上。

trailpackexpressv2.0.6现在已经修复了这个问题,请更新它,就不会再出现这个问题了

最新更新