title: Sequelize.js node.js many to many association
body: <p>I am using sequelize node.js module to form schema in Postgres sql</p>
  
  <p>I Have following two schema of Project and my users.</p>
  
  <p><strong>Project</strong></p>
  
  <pre><code>module.exports = function(sequelize, DataTypes) {
      var project = sequelize.define('project', {
          description: DataTypes.STRING
      },{
          classMethods: {
              associate: function(models) {
                  project.hasMany(models.myusers,{through:"project_persons"});
              }
          }
      });
  
      return project;
  };
  </code></pre>
  
  <p><strong>myusers</strong></p>
  
  <pre><code>    module.exports = function(sequelize, DataTypes) {
          var myusers = sequelize.define('myusers', {
          name: DataTypes.STRING
      },{
          classMethods: {
              associate: function(models) {
                  myusers.hasMany(models.project,{through:"project_persons"});
              }
          }
      });
  
      return myusers;
  };
  </code></pre>
  
  <p>i have trying to form a many to many relationship. When i run application third table "project_person is created".</p>
  
  <p>index.js (routes) i have the following code</p>
  
  <pre><code>models.myusers.create({
          description:"newuser"
      }).success(function(user) {
          var project1;
          var project2;
  
          models.project.create({
              description:"p1"
          }).success(function(p1){
              console.log("Project 1 created");
              project1 = p1;
          });
           models.project.create({
              description:"p2"
          }).success(function(p2){
              console.log("Project 2 created");
              project2 = p2;
          });
  
         user.setProjects([project1,project2]).success(function() {
                  console.log("CREATION SUCESS");
         });
      });
  </code></pre>
  
  <p>give me following error on the screen</p>
  
  <blockquote>
    <p>/home/hassan/Desktop/node/node_modules/sequelize/lib/associations/has-many-double-linked.js:151
    foreignIdentifier] = ((targetKeys.length === 1) ? unassociatedObject[targetKey
                                                                        ^
    TypeError: Cannot read property 'id' of undefined
        at /home/hassan/Desktop/node/node_modules/sequelize/lib/associations/has-many-double-linked.js:151:88
        at Array.map (native)
        at module.exports.HasManyDoubleLinked.injectSetter (/home/hassan/Desktop/node/node_modules/sequelize/lib/associations/has-many-double-linked.js:147:38)
        at instance.(anonymous function).success.proxy.events (/home/hassan/Desktop/node/node_modules/sequelize/lib/associations/has-many.js:317:39)
        at EventEmitter.emit (events.js:95:17)
        at module.exports.CustomEventEmitter.emit (/home/hassan/Desktop/node/node_modules/sequelize/lib/emitters/custom-event-emitter.js:107:33)
        at null. (/home/hassan/Desktop/node/node_modules/sequelize/lib/associations/has-many-double-linked.js:74:63)
        at EventEmitter.emit (events.js:95:17)
        at module.exports.CustomEventEmitter.emit (/home/hassan/Desktop/node/node_modules/sequelize/lib/emitters/custom-event-emitter.js:107:33)
        at emitter.query.success.error.proxy.events (/home/hassan/Desktop/node/node_modules/sequelize/lib/query-interface.js:890:19)
    24 Apr 10:31:47 - [nodemon] app crashed - waiting for file changes before starting...</p>
  </blockquote>
  
  <p>can any body tell where i am going wrong.</p>