不要怂,就是干,撸起袖子干!

Commit a65a3d6b by Mick Hansen

fix(associations/n:m): fix issues with inferred otherKey and association getters…

…/adders, closes #3185
1 parent dd326028
# Next
- [BUG] Fixed support for 2 x belongsToMany without foreignKey defined and association getter/adder [#3185](https://github.com/sequelize/sequelize/issues/3185)
# 2.0.3 # 2.0.3
- [BUG] Support for plain strings, ints and bools on JSON insert - [BUG] Support for plain strings, ints and bools on JSON insert
- [BUG] Fixed regression where `{$in: []}` would result in `IN ()` rather than `IN (NULL)` [#3105](https://github.com/sequelize/sequelize/issues/3105) [#3132](https://github.com/sequelize/sequelize/issues/3132) - [BUG] Fixed regression where `{$in: []}` would result in `IN ()` rather than `IN (NULL)` [#3105](https://github.com/sequelize/sequelize/issues/3105) [#3132](https://github.com/sequelize/sequelize/issues/3132)
......
...@@ -168,6 +168,8 @@ module.exports = (function() { ...@@ -168,6 +168,8 @@ module.exports = (function() {
delete this.through.model.rawAttributes[this.paired.otherKey]; delete this.through.model.rawAttributes[this.paired.otherKey];
} }
this.paired.otherKey = this.foreignKey; this.paired.otherKey = this.foreignKey;
this.paired.foreignIdentifier = this.foreignKey;
delete this.paired.foreignIdentifierField;
} }
} }
...@@ -289,6 +291,10 @@ module.exports = (function() { ...@@ -289,6 +291,10 @@ module.exports = (function() {
this.identifierField = this.through.model.rawAttributes[this.identifier].field || this.identifier; this.identifierField = this.through.model.rawAttributes[this.identifier].field || this.identifier;
this.foreignIdentifierField = this.through.model.rawAttributes[this.foreignIdentifier].field || this.foreignIdentifier; this.foreignIdentifierField = this.through.model.rawAttributes[this.foreignIdentifier].field || this.foreignIdentifier;
if (this.paired && !this.paired.foreignIdentifierField) {
this.paired.foreignIdentifierField = this.through.model.rawAttributes[this.paired.foreignIdentifier].field || this.paired.foreignIdentifier;
}
this.through.model.init(this.through.model.daoFactoryManager); this.through.model.init(this.through.model.daoFactoryManager);
Helpers.checkNamingCollision(this); Helpers.checkNamingCollision(this);
...@@ -315,10 +321,12 @@ module.exports = (function() { ...@@ -315,10 +321,12 @@ module.exports = (function() {
}.bind(this)); }.bind(this));
} }
options.where = new Utils.and([ options.where = {
scopeWhere, $and: [
options.where scopeWhere,
]); options.where
]
};
if (Object(through.model) === through.model) { if (Object(through.model) === through.model) {
throughWhere = {}; throughWhere = {};
......
...@@ -221,6 +221,48 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() { ...@@ -221,6 +221,48 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
expect(project.ProjectUsers.status).to.equal('active'); expect(project.ProjectUsers.status).to.equal('active');
}); });
}); });
it('supports custom primary keys and foreign keys', function () {
var User = this.sequelize.define('User', {
'iduser': {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: DataTypes.UUIDV4,
allowNull: false
}
});
var Group = this.sequelize.define('Group', {
'id_group': {
type: DataTypes.UUID,
allowNull: false,
primaryKey: true,
defaultValue: DataTypes.UUIDV4
}
});
var User_has_Group = this.sequelize.define('User_has_Group', {
});
User.belongsToMany(Group, {as: 'groups', through: User_has_Group, foreignKey: 'id_user'});
Group.belongsToMany(User, {as: 'users', through: User_has_Group, foreignKey: 'id_group'});
return this.sequelize.sync({force: true}).then(function () {
return Promise.join(
User.create(),
Group.create()
).spread(function (user, group) {
return user.addGroup(group);
}).then(function () {
return User.findOne({
where: {}
}).then(function (user) {
return user.getGroups();
});
});
});
});
}); });
describe('setAssociations', function() { describe('setAssociations', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!