1

I am setting up an API to a SQL Server 2008 database using Node, Feathers and Sequelize. I have successfully created the models and services using feathers-sequelize-auto, and most of the routes seem to be working. The app runs but with an error message:

Unhandled Rejection at: Promise  {"_bitField":18087936,"_fulfillmentHandler0":{}}

I'm getting an error for one of the routes (/project) relating to one of its foreign keys. Postman output for /project is:

{
    "name": "GeneralError",
    "message": "Invalid column name 'OrganisationID'.",
    "code": 500,
    "className": "general-error",
    "data": {},
    "errors": {}
} 

All works fine in the database itself, and I can run queries on the related tables with no issues.

Relevant parts of the Project.model.js:

Field definitions

LeadAgency: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
          model: 'Organisation',
          key: 'OrganisationID'
        }

Relationships:

Project.associate = function(models) {
    // Define associations here
    // See http://docs.sequelizejs.com/en/latest/docs/associations/
    Project.belongsTo(models.Team, { foreignKey: 'TeamID' });
    Project.belongsTo(models.Subteam, { foreignKey: 'SubTeamID' });
    Project.belongsTo(models.Staff, { foreignKey: 'StaffID' });
    Project.belongsTo(models.Organisation, { foreignKey: 'OrganisationID' });
    Project.belongsTo(models.Project, { foreignKey: 'ProjectID' });
  };

And this is the Organisation.model.js code:

/* jshint indent: 2 */

// See http://docs.sequelizejs.com/en/latest/docs/models-definition/
// for more of what you can do here.

const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;

module.exports = function(app) {
  const sequelizeClient = app.get('sequelizeClient');
  const Organisation = sequelizeClient.define(
    'Organisation',
    {
      OrganisationID: {
        type: DataTypes.INTEGER,
        allowNull: false,
        primaryKey: true,
        autoIncrement: true
      },
      Name: {
        type: DataTypes.STRING,
        allowNull: false
      },
      Type: {
        type: DataTypes.STRING,
        allowNull: true
      },
      AddressID: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
          model: 'Address',
          key: 'AddressID'
        }
      }
    },
    {
      tableName: 'Organisation',
      timestamps: false
    },
    {
      hooks: {
        beforeCount(options) {
          options.raw = true;
        }
      }
    }
  );

  // eslint-disable-next-line no-unused-vars
  Organisation.associate = function(models) {
    // Define associations here
    // See http://docs.sequelizejs.com/en/latest/docs/associations/
    Organisation.belongsTo(models.Address, { foreignKey: 'AddressID' });
  };

  return Organisation;
};

Noob here so could be missing something obvious. Help appreciated!

Kate
  • 35
  • 1
  • 11

1 Answers1

1

I got this error due to incorrect mappings in model.associate. In your Project model you have defined Project as associate also. This can be the reason.

Pramod
  • 706
  • 8
  • 8