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

Commit 54c06d49 by Jan Aagaard Meier

Fix for eager loading of paranoid children with non-paranoid parent. Closes #3050

1 parent d957be10
No preview for this file type
......@@ -718,7 +718,7 @@ module.exports = (function() {
}
mapFieldNames.call(this, options, this);
options = paranoidClause.call(this, options);
options = paranoidClause(this, options);
if (options.hooks) {
return this.runHooks('beforeFindAfterOptions', options);
......@@ -811,7 +811,7 @@ module.exports = (function() {
}
}
options = paranoidClause.call(this, options);
options = paranoidClause(this, options);
return this.QueryInterface.rawSelect(this.getTableName(options), options, aggregateFunction, this);
};
......@@ -1793,38 +1793,35 @@ module.exports = (function() {
// private
var paranoidClause = function(options) {
if (! this.options.timestamps || ! this.options.paranoid || options.paranoid === false) return options || {};
// validateIncludedElements should have been called before this method
var paranoidClause = function(model, options) {
options = options || {};
options.where = options.where || {};
// Apply on each include
// This should be handled before handling where conditions because of logic with returns
// otherwise this code will never run on includes of a already conditionable where
if (options.include && options.include.length) {
if (options.include) {
options.include.forEach(function(include) {
if (Utils._.isPlainObject(include) && include.model) {
paranoidClause.call(include.model, include);
}
paranoidClause(include.model, include);
});
}
var deletedAtCol = this._timestampAttributes.deletedAt
if (!model.options.timestamps || !model.options.paranoid || options.paranoid === false) {
// This model is not paranoid, nothing to do here;
return options;
}
var deletedAtCol = model._timestampAttributes.deletedAt
, deletedAtObject = {};
deletedAtObject[this.rawAttributes[deletedAtCol].field || deletedAtCol] = null;
deletedAtObject[model.rawAttributes[deletedAtCol].field || deletedAtCol] = null;
// detect emptiness
if(
Utils._.isObject(options.where) && Object.keys( options.where ).length === 0 ||
( Utils._.isString(options.where) || Utils._.isArray(options.where) ) && options.where.length === 0
) {
if (Utils._.isEmpty(options.where)) {
options.where = deletedAtObject;
return options;
} else {
options.where = model.sequelize.and(deletedAtObject, options.where);
}
options.where = this.sequelize.and( deletedAtObject, options.where );
return options;
};
......
......@@ -101,4 +101,41 @@ describe(Support.getTestDialectTeaser('Paranoid'), function() {
});
});
it('should not load paranoid, destroyed instances, with a non-paranoid parent', function () {
var X = this.sequelize.define('x', {
name: DataTypes.STRING
}, {
paranoid: false
});
var Y = this.sequelize.define('y', {
name: DataTypes.STRING
}, {
timestamps: true,
paranoid: true
});
X.hasMany(Y);
return this.sequelize.sync({ force: true}).bind(this).then(function () {
return this.sequelize.Promise.all([
X.create(),
Y.create()
]);
}).spread(function (x, y) {
this.x = x;
this.y = y;
return x.addY(y);
}).then(function () {
return this.y.destroy();
}).then(function () {
return X.findAll({
include: [Y]
}).get(0);
}).then(function (x) {
expect(x.ys).to.have.length(0);
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!