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

Commit 7f43820e by Mick Hansen

fix(model/attributes): make aliased primary keys for the main table work in case…

…s of a subquery select
1 parent 8f59f616
......@@ -624,15 +624,14 @@ module.exports = (function() {
}.bind(this)).join(', ');
if (subQuery && mainAttributes) {
if (model.hasPrimaryKeys) {
model.primaryKeyAttributes.forEach(function(keyAtt) {
if (mainAttributes.indexOf(keyAtt) === -1) {
mainAttributes.push(keyAtt);
}
});
} else {
mainAttributes.push('id');
}
model.primaryKeyAttributes.forEach(function(keyAtt) {
// Check if mainAttributes contain the primary key of the model either as a field or an aliased field
if (!_.find(mainAttributes, function (attr) {
return keyAtt === attr || keyAtt === attr[0] || keyAtt === attr[1];
})) {
mainAttributes.push(model.rawAttributes[keyAtt].field ? [keyAtt, model.rawAttributes[keyAtt].field] : keyAtt);
}
});
}
// Escape attributes
......@@ -793,8 +792,12 @@ module.exports = (function() {
, attrRight = association.identifier
, joinOn;
if (left.rawAttributes[attrLeft].field) {
attrLeft = left.rawAttributes[attrLeft].field;
// Alias the left attribute if the left attribute is not from a subqueried main table
// When doing a query like SELECT aliasedKey FROM (SELECT primaryKey FROM primaryTable) only aliasedKey is available to the join, this is not the case when doing a regular select where you can't used the aliased attribute
if (!subQuery || parentTable !== mainTableAs || tableLeft !== parentTable) {
if (left.rawAttributes[attrLeft].field) {
attrLeft = left.rawAttributes[attrLeft].field;
}
}
// Filter statement
......
......@@ -1482,11 +1482,9 @@ module.exports = (function() {
if (options.where) {
for (var attr in options.where) {
if (typeof attr === "string") {
if (Model.rawAttributes[attr] && Model.rawAttributes[attr].field) {
options.where[Model.rawAttributes[attr].field] = options.where[attr];
delete options.where[attr];
}
if (Model.rawAttributes[attr] && Model.rawAttributes[attr].field) {
options.where[Model.rawAttributes[attr].field] = options.where[attr];
delete options.where[attr];
}
}
}
......
......@@ -148,7 +148,7 @@ describe(Support.getTestDialectTeaser("Model"), function () {
});
});
it('should work with attributes and where on includes for find', function () {
it('should work with where on includes for find', function () {
var self = this;
return this.User.create({
......@@ -167,16 +167,16 @@ describe(Support.getTestDialectTeaser("Model"), function () {
{model: self.Comment},
{model: self.User}
],
where: {title: 'DoDat'}
where: {title: 'DatDo'}
});
}).then(function (task) {
expect(task.get('title')).to.equal('DoDat');
expect(task.get('comments'))[0].to.equal('Comment');
expect(task.get('title')).to.equal('DatDo');
expect(task.get('comments')[0].get('text')).to.equal('Comment');
expect(task.get('user')).to.be.ok;
});
});
it('should work with attributes and where on includes for findAll', function () {
it('should work with where on includes for findAll', function () {
var self = this;
return this.User.create({
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!