Search code examples
node.jssequelize.jstrailsjs

GET request fails on a model with string 'id' if the 'id' begins with a number - Trailsjs


I have a model called Product with two fields id and name. I have footprints installed.

'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'
      }
    }
  }
}

When I use postman and GET request localhost:3000/product?id=XX2525 the sql generated in the logger is

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

If I do the same query on an id that begins with a number localhost:3000/product?id=10XX2525 the sql generated is

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

I am not sure if that is a trails thing or a sequelize thing but if I have my field defined as a string in my model I would expect the query to search for as tring and not to apply any conversions. The error looks like (001CAR is the first id in my database):

{
    "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": {}
    }
}

My package.json dependencies are and I am running 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"
  }

Solution

  • You run into a bug that is already fixed by this PR https://github.com/trailsjs/trailpack/pull/44 but the fix is not deployed apparently on npm.

    trailpack-express v2.0.6 have this fix now, update it and you shouldn't have the problem anymore