title: Sequelize - Cannot add or update a child row: a foreign key constraint fails
body: <p>I have an issue where I am not able to use a specific object value to create a record. While it executes the insertion command, an error is thrown immediately after the command begins and a record is never created. The error comes at <code>userId:user.user_id</code> in my route.</p>
  
  <pre><code>Unhandled rejection SequelizeForeignKeyConstraintError: ER_NO_REFERENCED_ROW_2: Cannot add or update a child row: a foreign key constraint fails (`test_db`.`member`, CONSTRAINT `member_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `organization` (`organization_id`) ON DELETE CASCADE ON UPDATE CASCADE)
  </code></pre>
  
  <p>Here is the relationship on the model:</p>
  
  <p><a href="https://i.stack.imgur.com/fIK1e.png" rel="noreferrer"><img src="https://i.stack.imgur.com/fIK1e.png" alt="member-model"></a></p>
  
  <p>Here is the logged SQL:</p>
  
  <pre><code>Executing (default): INSERT INTO `user` (`user_id`,`email`,`organization_id`,`authentication_token`,`updatedAt`,`createdAt`) VALUES (DEFAULT,'tester@vdfd.com','1','f800127f4b345d1af54d23f566eca88859898d70','2016-05-30 21:39:19','2016-05-30 21:39:19');
  This user:[object SequelizeInstance:user]
  41
  Executing (default): INSERT INTO `member` (`member_id`,`member_email`,`organization_id`,`user_id`,`updatedAt`,`createdAt`) VALUES (DEFAULT,'tester@vdfd.com','1',41,'2016-05-30 21:39:19','2016-05-30 21:39:19');
  </code></pre>
  
  <p><strong>Route:</strong></p>
  
  <pre><code>.post(function(req, res, user){
  
          var token;
  
          User.create({
                  email: req.body.email,
                  organizationId: req.body.organizationId,
                  authenticationToken: crypto.randomBytes(20).toString('hex')
          }).then(function(user){
              token = user.authenticationToken;
              console.log('This user:' + user);
              console.log(user.user_id);
              return Member.create({
                  organizationId: req.body.organizationId,
                  memberEmail: req.body.email,
                  userId: user.user_id
              });
          }).then(function(authToken){
              console.log(authToken);
              console.log("Success");
              res.redirect('/app/settings/add-users');
          })  
      });
  </code></pre>
  
  <p><strong>member.js:</strong></p>
  
  <pre><code>module.exports = function(sequelize, DataTypes) {
  
  var Member = sequelize.define('member', {
      memberId: {
          type: DataTypes.INTEGER,
          field: 'member_id',
          autoIncrement: true,
          primaryKey: true
      },
      memberEmail: {
          type: DataTypes.STRING,
          field: 'member_email',
          isEmail: true,
          unique: true
      },
      organizationId: {
          type: DataTypes.INTEGER,
          field: 'organization_id',
          allowNull: true
      },
      userId: {
          type: DataTypes.INTEGER,
          field: 'user_id',
          allowNull: true
      }
  },{
      freezeTableName: true,
  });
      return Member;
  }
  </code></pre>
  
  <p><strong>user.js:</strong></p>
  
  <pre><code>var bcrypt   = require('bcrypt-nodejs');
  
  module.exports = function(sequelize, DataTypes) {
  
  var User = sequelize.define('user', {
      user_id: {
          type: DataTypes.INTEGER,
          autoIncrement: true,
          primaryKey: true
      },
      firstName: {
          type: DataTypes.STRING,
          field: 'first_name'
      },
      lastName: {
          type: DataTypes.STRING,
          field: 'last_name'
      },
      email: {
          type: DataTypes.STRING,
          isEmail: true,
          unique: true,
          set: function(val) {
              this.setDataValue('email', val.toLowerCase());
          }
      },
      password: DataTypes.STRING,
      organizationId: {
          type: DataTypes.INTEGER,
          field: 'organization_id',
          allowNull: true
      },
      authenticationToken: {
          type: DataTypes.STRING,
          field: 'authentication_token'
      },
      resetPasswordToken: {
          type: DataTypes.STRING,
          field: 'reset_password_token'
      },
      resetPasswordExpires: {
          type: DataTypes.DATE,
          field: 'reset_password_expires'
      }
  }, {
      freezeTableName: true,
      classMethods: {
          associate: function(db) {
              User.belongsToMany(db.Organization, { through: 'member', foreignKey: 'organizationId'})
          },
          generateHash: function(password) {
              return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
          },
      },
      instanceMethods: {
          validPassword: function(password) {
              return bcrypt.compareSync(password, this.password);
          },
      },  
  });
      return User;
  }
  </code></pre>
  
  <p><strong>organization.js:</strong></p>
  
  <pre><code>module.exports = function(sequelize, DataTypes) {
  
  var Organization = sequelize.define('organization', {
      organizationId: {
          type: DataTypes.INTEGER,
          field: 'organization_id',
          autoIncrement: true,
          primaryKey: true
      },
      organizationName: {
          type: DataTypes.STRING,
          field: 'organization_name'
      },
      admin: DataTypes.STRING
  },{
      freezeTableName: true,
      classMethods: {
          associate: function(db) {
              Organization.belongsToMany(db.User, { through: 'member', foreignKey: 'user_id' }),
          },
      }
  });
      return Organization;
  }
  </code></pre>