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

GET request fails on a model with string 'id' if the 'id' begins with a number - 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'
      }
    }
  }
}

当我使用 postman 和 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 的东西,但是如果我在我的模型中将我的字段定义为字符串,我希望查询搜索为 tring 而不是应用任何转换。错误看起来像(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 依赖项是 运行 node v6.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 上。

trailpack-express v2.0.6 现在有这个修复,更新它,你应该不会再有这个问题了