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

Commit 1d8aff72 by Jan Aagaard Meier

Merge pull request #2372 from vpontis/puppy-branch

single linked has many broken when specifying field
2 parents 8cd9033a 635c81b0
......@@ -1235,7 +1235,7 @@ module.exports = (function() {
if (smth.attribute._isSequelizeMethod) {
key = this.getWhereConditions(smth.attribute, tableName, factory, options, prepend);
} else {
key = this.quoteTable(smth.attribute.Model.name) + '.' + this.quoteIdentifier(smth.attribute.fieldName);
key = this.quoteTable(smth.attribute.Model.name) + '.' + this.quoteIdentifier(smth.attribute.field || smth.attribute.fieldName);
}
if (value._isSequelizeMethod) {
......
......@@ -1476,7 +1476,9 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
beforeEach(function() {
this.User = this.sequelize.define('User', { name: DataTypes.STRING });
this.Project = this.sequelize.define('Project', { name: DataTypes.STRING });
this.Puppy = this.sequelize.define('Puppy', { breed: DataTypes.STRING });
// doubly linked has many
this.User.hasMany(this.Project, {
through: 'user_projects',
as: 'Projects',
......@@ -1493,9 +1495,24 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
name: 'projectId'
}
});
// singly linked has many
this.User.hasMany(this.Puppy, {
as: 'Puppies',
foreignKey: {
field: 'user_id',
name: 'userId'
}
});
this.Puppy.belongsTo(this.User, {
foreignKey: {
field: 'user_id',
name: 'userId'
}
});
});
it('should correctly get associations', function() {
it('should correctly get associations when doubly linked', function() {
var self = this;
return this.sequelize.sync({force: true}).then(function() {
return Promise.all([
......@@ -1513,6 +1530,26 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
});
});
it('should correctly get associations when singly linked', function() {
var self = this;
return this.sequelize.sync({force: true}).then(function() {
return Promise.all([
self.User.create({name: 'Matt'}),
self.Puppy.create({breed: 'Terrier'})
]);
}).spread(function (user, puppy) {
return user.addPuppy(puppy).return(user);
}).then(function(user) {
return user.getPuppies().then(function(puppies) {
var puppy = puppies[0];
expect(puppy).to.be.defined;
expect(puppy.rawAttributes.userId).to.be.ok
expect(puppy.userId).to.equal(user.id)
});
});
});
it('should be able to handle nested includes properly', function() {
var self = this;
this.Group = this.sequelize.define('Group', { groupName: DataTypes.STRING});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!