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

Commit 52a1f1a0 by Mick Hansen

Merge branch 'haches-master'

2 parents 3cc67019 314d21ea
...@@ -106,10 +106,10 @@ module.exports = (function() { ...@@ -106,10 +106,10 @@ module.exports = (function() {
} }
// If through is not default, determine pairing by through value (model/string) // If through is not default, determine pairing by through value (model/string)
else { else {
paired = self.options.through === association.options.through || paired = (self.options.through === association.options.through) ||
self.options.through === (association.options.through && association.options.through.model) || (self.options.through === association.options.through.model) ||
(self.options.through && self.options.through.model) === (association.options.through && association.options.through.model) || (self.options.through.model && association.options.through.model && (self.options.through.model === association.options.through.model)) ||
(self.options.through && self.options.through.model) === association.options.through; (self.options.through.model === association.options.through);
} }
// If paired, set properties identifying both associations as double linked, and allow them to each eachtoerh // If paired, set properties identifying both associations as double linked, and allow them to each eachtoerh
if (paired) { if (paired) {
...@@ -294,7 +294,6 @@ module.exports = (function() { ...@@ -294,7 +294,6 @@ module.exports = (function() {
if (this.targetAssociation.foreignIdentifier) { if (this.targetAssociation.foreignIdentifier) {
this.targetAssociation.foreignIdentifierField = this.through.model.rawAttributes[this.targetAssociation.foreignIdentifier].field || this.targetAssociation.foreignIdentifier; this.targetAssociation.foreignIdentifierField = this.through.model.rawAttributes[this.targetAssociation.foreignIdentifier].field || this.targetAssociation.foreignIdentifier;
} }
this.through.model.init(this.through.model.daoFactoryManager); this.through.model.init(this.through.model.daoFactoryManager);
} else { } else {
var newAttributes = {}; var newAttributes = {};
......
...@@ -2080,6 +2080,46 @@ describe(Support.getTestDialectTeaser("HasMany"), function() { ...@@ -2080,6 +2080,46 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
expect(Group.associations.MyUsers.through.model.rawAttributes.GroupId).to.exist; expect(Group.associations.MyUsers.through.model.rawAttributes.GroupId).to.exist;
}); });
}); });
describe('multiple hasMany', function() {
beforeEach(function() {
this.User = this.sequelize.define('user', { name: Sequelize.STRING });
this.Project = this.sequelize.define('project', { projectName: Sequelize.STRING });
});
describe('project has owners and users and owners and users have projects', function() {
beforeEach(function() {
this.Project.hasMany(this.User, { as: 'owners', through: 'projectOwners'});
this.Project.hasMany(this.User, { as: 'users', through: 'projectUsers'});
this.User.hasMany(this.Project, { as: 'ownedProjects', through: 'projectOwners'});
this.User.hasMany(this.Project, { as: 'memberProjects', through: 'projectUsers'});
return this.sequelize.sync({ force: true });
});
it('correctly pairs associations', function () {
expect(this.Project.associations.owners.targetAssociation).to.equal(this.User.associations.ownedProjects);
expect(this.Project.associations.users.targetAssociation).to.equal(this.User.associations.memberProjects);
});
it('correctly sets user and owner', function() {
var self = this;
var p1 = this.Project.build({ projectName: 'p1' })
, u1 = this.User.build({ name: 'u1' })
, u2 = this.User.build({ name: 'u2' });
return p1
.save()
.then(function() { return u1.save(); })
.then(function() { return u2.save(); })
.then(function() { return p1.setUsers([u1]); })
.then(function() { return p1.setOwners([u2]); });
});
});
});
}); });
describe("Foreign key constraints", function() { describe("Foreign key constraints", function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!