Fix #1079 - Polymorphic hasMany inverse relation (#1296)

This fixes the polymorphic hasMany inverse relation
not working as expected in include filters.
This commit is contained in:
Janny 2017-07-11 15:47:32 -04:00 committed by GitHub
parent da323964f5
commit 6d017c1e34
3 changed files with 33 additions and 3 deletions

View File

@ -18,5 +18,3 @@ Also install the appropriated connector, for example for mongodb:
npm install loopback-connector-mongodb
See [StrongLoop Documentation](http://docs.strongloop.com/) for more information.

View File

@ -317,10 +317,14 @@ Inclusion.include = function(objects, include, options, cb) {
inq: uniq(sourceIds),
};
if (polymorphic) {
var throughModel = polymorphic.invert ?
relation.modelTo :
relation.modelFrom;
//handle polymorphic hasMany (reverse) in which case we need to filter
//by discriminator to filter other types
throughFilter.where[polymorphic.discriminator] =
relation.modelFrom.definition.name;
throughModel.definition.name;
}
/**
* 1st DB Call of 2 step process. Get through model objects first

View File

@ -5609,4 +5609,32 @@ describe('relations', function() {
}).should.throw('Invalid relation name: trigger');
});
});
describe('polymorphic hasMany', function() {
before(function(done) {
Picture = db.define('Picture', {name: String});
Author = db.define('Author', {name: String});
PictureLink = db.define('PictureLink', {});
Author.hasMany(Picture, {through: PictureLink, polymorphic: 'imageable', invert: true});
Picture.hasMany(Author, {through: PictureLink, polymorphic: 'imageable'});
db.automigrate(['Picture', 'Author', 'PictureLink'], done);
});
it('should properly query through an inverted relationship', function(done) {
Author.create({name: 'Steve'}, function(err, author) {
if (err) { return done(err); }
author.pictures.create({name: 'Steve pic 1'}, function(err, pic) {
if (err) { return done(err); }
Author.findOne({include: 'pictures'}, function(err, author) {
if (err) { return done(err); }
author.pictures().length.should.eql(1);
author.pictures()[0].name.should.eql('Steve pic 1');
done();
});
});
});
});
});
});