cphill
cphill

Reputation: 5914

Sequelize CLI Migration classMethod Change

I am trying to update the relationship between tables in my model files that have a many-to-many relationship. I am currently getting errors with a command that I am trying to use to the default nature that the relationship must be unique. As a result I want to make the simple adjustment of adding a property to my belongsToMany with unique: false, but I am not sure the proper format to use in the migration file. There doesn't seem to be any documentation on a queryInterface command for changing classMethod. Do I even need a migration file?

I want to change this:

classMethods: {
        associate: function(db) {
            User.belongsToMany(db.Organization, { through: 'member', foreignKey: 'user_id'}),
            User.belongsToMany(db.Team, { through: 'member', foreignKey: 'user_id'})
        },

to this (unique: false)

classMethods: {
        associate: function(db) {
            User.belongsToMany(db.Organization, { through: 'member', unique: false, foreignKey: 'user_id'}),
            User.belongsToMany(db.Team, { through: 'member', unique: false, foreignKey: 'user_id'})
        },

Upvotes: 0

Views: 221

Answers (1)

Eric Ihli
Eric Ihli

Reputation: 1907

Don't know if this is your problem but sequelize-cli's model:create generates model definitions the old way. classMethods has been deprecated as of sequelize v4. http://docs.sequelizejs.com/manual/tutorial/upgrade-to-v4.html

Old way:

module.exports = function(sequelize, DataTypes) {
  var Profile = sequelize.define('profile', {
    bio: DataTypes.TEXT,
    email: DataTypes.STRING
  }, {
    classMethods: {
      associate: function(models) {
        // associations can be defined here
        Profile.belongsTo(models.user);
      }
    }
  });
  return Profile;
};

New way:

module.exports = function(sequelize, DataTypes) {
  var Profile = sequelize.define('profile', {
    bio: DataTypes.TEXT,
    email: DataTypes.STRING
  });

  Profile.associate = function(models) {
    Profile.belongsTo(models.user);
  };

  return Profile;
};

Upvotes: 2

Related Questions