generator-angular-express-sequelize icon indicating copy to clipboard operation
generator-angular-express-sequelize copied to clipboard

Associations in Sequelize

Open tcaraccia opened this issue 11 years ago • 2 comments
trafficstars

How should I create an association within boilerplate ?

module.exports = function(sequelize, DataTypes) { var Account = sequelize.define('Account', { name: { type: DataTypes.STRING, validate: { notNull: true, }, }, type: { type: DataTypes.ENUM('Customer', 'Provider', 'Employees'), validate: { notNull: true, },

},

}) Account.hasMany(Transactions); /// ??????

return Account }

tcaraccia avatar May 12 '14 19:05 tcaraccia

You add classMethods object . The sequelize page about express http://sequelizejs.com/articles/express#models-user-js

module.exports = function(sequelize, DataTypes) {
  var Account = sequelize.define('Account', {
    name: {
      type: DataTypes.STRING
    }
  },

  {
    classMethods: {
      associate: function(models) {
        Account.hasMany(models.Transactions)
      }
    }
  })

  return Account
}

wykhuh avatar Dec 11 '14 23:12 wykhuh

@wykhuh the only working code I found is:

module.exports = function(sequelize, DataTypes) {
  var Account = sequelize.define('Account', {
    name: {
      type: DataTypes.STRING
    }
  },

  {
      associate: function(models) {
        Account.hasMany(models.Transactions)
      }
  })

  return Account
}

lvbeck avatar May 13 '15 12:05 lvbeck