Merge branch 'walkonsocial-hasmanythrough-inconsitency'

This commit is contained in:
Raymond Feng 2015-04-24 16:11:16 -07:00
commit 73f98b3ae1
2 changed files with 58 additions and 2 deletions

View File

@ -865,7 +865,13 @@ var throughKeys = function(definition) {
var fk2 = definition.keyThrough;
}
} else if (definition.modelFrom === definition.modelTo) {
return findBelongsTo(modelThrough, definition.modelTo, pk2);
return findBelongsTo(modelThrough, definition.modelTo, pk2).
sort(function (fk1, fk2) {
//Fix for bug - https://github.com/strongloop/loopback-datasource-juggler/issues/571
//Make sure that first key is mapped to modelFrom
//& second key to modelTo. Order matters
return (definition.keyTo === fk1) ? -1 : 1;
});
} else {
var fk1 = findBelongsTo(modelThrough, definition.modelFrom,
definition.keyFrom)[0];

View File

@ -1214,7 +1214,57 @@ describe('relations', function () {
});
});
describe('hasMany through - between same model', function () {
describe('hasMany through bi-directional relations on the same model', function () {
var User, Follow, Address;
before(function (done) {
db = getSchema();
User = db.define('User', {name: String});
Follow = db.define('Follow', {date: {type: Date,
default: function () {
return new Date();
}}});
Address = db.define('Address', {name: String});
User.hasMany(User, {as: 'followers', foreignKey: 'followeeId', keyThrough: 'followerId', through: Follow});
User.hasMany(User, {as: 'following', foreignKey: 'followerId', keyThrough: 'followeeId', through: Follow});
User.belongsTo(Address);
Follow.belongsTo(User, {as: 'follower'});
Follow.belongsTo(User, {as: 'followee'});
db.automigrate(['User', 'Follow'], function (err) {
done(err);
});
});
it('should set foreignKeys of through model correctly in first relation',
function (done) {
var follower = new User({id: 1});
var followee = new User({id: 2});
followee.followers.add(follower, function (err, throughInst) {
should.not.exist(err);
should.exist(throughInst);
throughInst.followerId.should.equal(follower.id);
throughInst.followeeId.should.equal(followee.id);
done();
});
});
it('should set foreignKeys of through model correctly in second relation',
function (done) {
var follower = new User({id: 3});
var followee = new User({id: 4});
follower.following.add(followee, function (err, throughInst) {
should.not.exist(err);
should.exist(throughInst);
throughInst.followeeId.should.equal(followee.id);
throughInst.followerId.should.equal(follower.id);
done();
});
});
});
describe('hasMany through - between same models', function () {
var User, Follow, Address;
before(function (done) {