Friday 14 April 2017

Sequelize Association Columns not generated

I'm trying to associate Products model with User model while making sure to force sync in order to ensure that the newest changes are present. I get the error that columns UserId and ProductManagerId are not created.

Why are the columns not auto-generated?

Product.js

'use strict';
/* Model ONLY for DePuy Products */
module.exports = function(sequelize, DataTypes) {
  var Product = sequelize.define('Product', {
    name: {
      type: DataTypes.STRING,
      allowNull : false
    },
    sku: {
      type:DataTypes.STRING,
      allowNull: false,
      primaryKey: true,
    },
  }, {
    classMethods: {
      associate: function(models) {
        Product.belongsTo(models.User,{ as : 'ProductManager'});
        // associations can be defined here
      }
    }
  });
      Product.sync({force: true}).then(() => {
        console.log('Sequelize forced');
      })

  return Product;
};

User.js

module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define('User', {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true,
      allowNull: false,
    },
    password: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    email: {
      type: DataTypes.STRING,
      unique: true,
    },
  }, {
    classMethods: {
      associate: (models) => {
        // associations can be defined here
        User.hasMany(models.Product);
      },
    },
    freezeTableName: true,
  });
  // FORCE FOR NOW Use only if make model changes
    User.sync({force: true}).then(() => {
     console.log('Sequelize forced');
   })
  return User;
};

Dialect: Postgres Sequelize version: 3.30.1

Ps. I originally used sequelize-cli to generate the model file, then changed to my needs.



via Turtle

No comments:

Post a Comment