Repository: sequelize/sequelize

Issue #3855: Instance#reload not working correctly on { through: Model }
Created: 2015-06-02T22:17:16Z
When having a join model as through table using belongsToMany the instance doesn't get reloaded correctly.
Definitions and associations:
var GiftTableProducts = sequelize.define('GiftTableProducts', {
    quantity: DataTypes.FLOAT
});

var Product = sequelize.define('Product', {
    // ... attrs ...
});

var GiftTable = sequelize.define('GiftTable', {
    // ... attrs ...
});

GiftTable.belongsToMany(Product, { through: GiftTableProducts});
Product.belongsToMany(GiftTable, { through: GiftTableProducts });
GiftTableProducts.belongsTo(GiftTable, { onDelete: 'CASCADE' });
GiftTableProducts.belongsTo(Product, { onDelete: 'CASCADE' });


The GiftTableProducts table definition is the following:

When updating some instance, let's say

GiftTableProducts.find({where: { GiftTableId: '2', ProductId: 1}})
.then(function (gift) {
    gift.increment(['quantity'], { by: '10' })
    .then(obj.reload({ GiftTableId: 2, ProductId: 1 }))
    .then(function (obj) { res.send(obj) });
});

Produces the following executions:
SELECT `quantity`, `createdAt`, `updatedAt`, `ProductId`, `GiftTableId` FROM `GiftTableProducts` AS `GiftTableProducts` WHERE `GiftTableProducts`.`GiftTableId` = '2' AND `GiftTableProducts`.`ProductId` = 1;

UPDATE `GiftTableProducts` SET `quantity`=`quantity` + '10',`updatedAt`='2015-06-02 19:52:11' WHERE `ProductId` = 1 AND `GiftTableId` = 2

SELECT `quantity`, `createdAt`, `updatedAt`, `ProductId`, `GiftTableId` FROM `GiftTableProducts` AS `GiftTableProducts` WHERE `GiftTableProducts`.`ProductId`=1 LIMIT 1;


The WHERE clause on the reload SELECT doesn't include the corresponding ProductId which is a primary key even when passing by the { GiftTableId: 2, ProductId: 1 } object to the .reload call returning the first gift it founds having the same GiftTableId and maybe not being the same one updated.

Duplicate issue #3973: Instance.reload() fails if instance was fetched with many-to-many association included
Created: 2015-06-19T14:58:01Z
I have two models, Meetings and Participants, in a many-to-many relationship. Reloading an instance fails if the association has been included in findOne()/findAll():
db.Meeting.findOne({ include: [db.Participant] })
.then(function(result) {
  return result.reload(); // fails (works fine if { include: [db.Participant] } is omitted)
});

Unhandled rejection TypeError: Cannot read property 'name' of undefined                                                                               
    at C:\Users\username\src\playground\many-to-many\node_modules\sequelize\lib\model.js:275:39                                                           
    at Array.map (native)                                                                                                                             
    at conformOptions (C:\Users\username\src\playground\many-to-many\node_modules\sequelize\lib\model.js:259:37)                                          
    at C:\Users\username\src\playground\many-to-many\node_modules\sequelize\lib\model.js:291:7                                                            
    at Array.map (native)                                                                                                                             
    at conformOptions (C:\Users\username\src\playground\many-to-many\node_modules\sequelize\lib\model.js:259:37)                                          
    at null.<anonymous> (C:\Users\username\src\playground\many-to-many\node_modules\sequelize\lib\model.js:1162:5)                                        
    at tryCatcher (C:\Users\username\src\playground\many-to-many\node_modules\sequelize\node_modules\bluebird\js\main\util.js:24:31)                      
    at Promise._settlePromiseFromHandler (C:\Users\username\src\playground\many-to-many\node_modules\sequelize\node_modules\bluebird\js\main\promise.js:454:31)                                                                                                            
    at Promise._settlePromiseAt (C:\Users\username\src\playground\many-to-many\node_modules\sequelize\node_modules\bluebird\js\main\promise.js:530:18)    
    at Promise._settlePromiseAtPostResolution (C:\Users\username\src\playground\many-to-many\node_modules\sequelize\node_modules\bluebird\js\main\promise.js:224:10)                                                                                                                                   
    at Async._drainQueue (C:\Users\username\src\playground\many-to-many\node_modules\sequelize\node_modules\bluebird\js\main\async.js:187:12)             
    at Async._drainQueues (C:\Users\username\src\playground\many-to-many\node_modules\sequelize\node_modules\bluebird\js\main\async.js:192:10)            
    at Immediate.Async.drainQueues [as _onImmediate] (C:\Users\username\src\playground\many-to-many\node_modules\sequelize\node_modules\bluebird\js\main\async.js:15:14)                                                                                                                                
    at processImmediate [as _immediateCallback] (timers.js:358:17)                                                                                    

My models are
module.exports = function(sequelize, DataTypes) {
  var Participant = sequelize.define('Participant', {
    firstName: DataTypes.STRING
  }, {
    classMethods: {
      associate: function(models) {
        models.Participant.belongsToMany(
          models.Meeting, 
          { through: 'MeetingParticipants' }
        );
      }
    }
  });
  return Participant;
};

module.exports = function(sequelize, DataTypes) {
  var Meeting = sequelize.define('Meeting', {
    description: DataTypes.STRING
  }, {
    classMethods: {
      associate: function(models) {
        models.Meeting.belongsToMany(
          models.Participant, 
          { through: 'MeetingParticipants' }
        );
      }
    }
  });
  return Meeting;
};

Closing comment (2015-08-22T22:58:15Z): I'm reloading instances having belongsToMany associations having a model defined for the through property (not as string).
Also, you might need to define explicitly an id as primary key for that model on the association.
Maybe also it's a duplicate of #3855