Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix morphTo options.query #2059

Merged
merged 5 commits into from Mar 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/eager.js
Expand Up @@ -58,8 +58,9 @@ class EagerRelation extends EagerBase {
const idAttribute = _.result(Target.prototype, 'idAttribute');
const ids = getAttributeUnique(parents, idColumn);

// Remove `query` from options to not send the same query to all candidates
return Target.query('whereIn', idAttribute, ids)
.sync(options)
.sync(_.assign({}, options, {query: undefined}))
.select()
.tap((response) => {
const clone = relatedData.instance('morphTo', Target, {
Expand Down
45 changes: 45 additions & 0 deletions test/integration/helpers/objects.js
Expand Up @@ -409,7 +409,52 @@ module.exports = function(Bookshelf) {
}
});

function generateEventModels(eventHooks) {
var Photo = Bookshelf.Model.extend({
tableName: 'photos',
imageable: function() {
return this.morphTo('imageable', Site, [Author, 'profile_pic']);
}
});

var Site = Bookshelf.Model.extend({
tableName: 'sites',
photos: function() {
return this.morphMany(Photo, 'imageable');
},
initialize: function() {
this.constructor.__super__.initialize.apply(this, arguments);
this.on('fetching', function(model, columns, options) {
eventHooks.fetching('sites', model, columns, options);
});
}
});

var Author = Bookshelf.Model.extend({
tableName: 'authors',
site: function() {
return this.belongsTo(Site);
},
photo: function() {
return this.morphOne(Photo, 'imageable', 'profile_pic');
},
initialize: function() {
this.constructor.__super__.initialize.apply(this, arguments);
this.on('fetching', function(model, columns, options) {
eventHooks.fetching('authors', model, columns, options);
});
}
});

return {
Photo,
Site,
Author
};
}

return {
generateEventModels,
Models: {
Site: Site,
SiteParsed: SiteParsed,
Expand Down
15 changes: 15 additions & 0 deletions test/integration/relations.js
Expand Up @@ -1425,5 +1425,20 @@ module.exports = function(Bookshelf) {
});
});
});

describe('PR #2059 - opts.query on fetching with morphTo', function() {
it('should correctly set query on fetching with morphTo', async function() {
const {Photo} = objs.generateEventModels({
fetching: function(table, model, columns, options) {
// Check that options.query actually queries this table
equal(options.query._single.table, table);
}
});

// Execute a query that will trigger fetching events
// These have assertions
return Photo.forge().fetchAll({withRelated: 'imageable'});
});
});
});
};