2

我有一个名为 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'
      }
    }
  }
}

当我使用邮递员和 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';

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

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

我不确定这是追踪还是续集,但如果我在模型中将字段定义为字符串,我希望查询搜索为字符串而不应用任何转换。错误看起来像(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 依赖项是并且我正在运行节点 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"
  }
4

1 回答 1

2

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

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

于 2017-10-31T07:28:23.300 回答