rayokota / generator-angular-express-sequelize

Yeoman generator for AngularJS + Express + Sequelize
MIT License
81 stars 21 forks source link

Associations in Sequelize #4

Open tcaraccia opened 10 years ago

tcaraccia commented 10 years ago

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 }

wykhuh commented 9 years ago

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
}
lvbeck commented 9 years ago

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