Created
June 3, 2015 06:40
-
-
Save matmar10/a2fc0ecc94e2840b9f9a to your computer and use it in GitHub Desktop.
Sequelize issue #3433 partial fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/instance.js b/lib/instance.js | |
index 5668513..688f286 100644 | |
--- a/lib/instance.js | |
+++ b/lib/instance.js | |
@@ -737,6 +737,7 @@ module.exports = (function() { | |
* | |
* @see {Model#find} | |
* @param {Object} [options] Options that are passed on to `Model.find` | |
+ * @param {Array} [options.include] List of joined models to include. If no include option is provided, uses the include option provided to instance in previous `Model.find` | |
* @return {Promise<this>} | |
*/ | |
Instance.prototype.reload = function(options) { | |
@@ -744,12 +745,13 @@ module.exports = (function() { | |
, where = [ | |
this.QueryInterface.quoteTable(this.Model.name) + '.' + this.QueryInterface.quoteIdentifier(this.Model.primaryKeyField) + '=?', | |
this.get(this.Model.primaryKeyAttribute, {raw: true}) | |
- ]; | |
+ ] | |
+ , include = (options) ? options.include || null : this.options.include || null; | |
return this.Model.find({ | |
where: where, | |
limit: 1, | |
- include: this.options.include || null | |
+ include: include | |
}, options).then(function(reload) { | |
self.set(reload.dataValues, {raw: true, reset: true}); | |
}).return(self); | |
diff --git a/test/integration/instance.test.js b/test/integration/instance.test.js | |
index 6c16bb4..8d40730 100644 | |
--- a/test/integration/instance.test.js | |
+++ b/test/integration/instance.test.js | |
@@ -597,6 +597,33 @@ describe(Support.getTestDialectTeaser('Instance'), function() { | |
}); | |
}); | |
}); | |
+ | |
+ it('should honor include options specified during reload', function() { | |
+ var Book = this.sequelize.define('Book', { title: DataTypes.STRING }) | |
+ , Page = this.sequelize.define('Page', { content: DataTypes.TEXT }); | |
+ | |
+ Book.hasMany(Page); | |
+ Page.belongsTo(Book); | |
+ | |
+ return Book.sync({force: true}).then(function() { | |
+ return Page.sync({force: true}).then(function() { | |
+ return Book.create({ title: 'A very old book' }).then(function(book) { | |
+ return Page.create({ content: 'om nom nom' }).then(function(page) { | |
+ return book.setPages([page]).then(function() { | |
+ return Book.find({ | |
+ where: { id: book.id } | |
+ }).then(function(leBook) { | |
+ return leBook.reload({ include: [{ model: Page }] }).then(function(leBook) { | |
+ expect(leBook.Pages.length).to.equal(1); | |
+ expect(leBook.Pages[0].content).to.equal('om nom nom'); | |
+ }); | |
+ }); | |
+ }); | |
+ }); | |
+ }); | |
+ }); | |
+ }); | |
+ }); | |
}); | |
describe('default values', function() { | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment