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

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() { ...@@ -624,15 +624,14 @@ module.exports = (function() {
}.bind(this)).join(', '); }.bind(this)).join(', ');
if (subQuery && mainAttributes) { if (subQuery && mainAttributes) {
if (model.hasPrimaryKeys) {
model.primaryKeyAttributes.forEach(function(keyAtt) { model.primaryKeyAttributes.forEach(function(keyAtt) {
if (mainAttributes.indexOf(keyAtt) === -1) { // Check if mainAttributes contain the primary key of the model either as a field or an aliased field
mainAttributes.push(keyAtt); 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);
} }
}); });
} else {
mainAttributes.push('id');
}
} }
// Escape attributes // Escape attributes
...@@ -793,9 +792,13 @@ module.exports = (function() { ...@@ -793,9 +792,13 @@ module.exports = (function() {
, attrRight = association.identifier , attrRight = association.identifier
, joinOn; , joinOn;
// 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) { if (left.rawAttributes[attrLeft].field) {
attrLeft = left.rawAttributes[attrLeft].field; attrLeft = left.rawAttributes[attrLeft].field;
} }
}
// Filter statement // Filter statement
// Used by both join and subquery where // Used by both join and subquery where
......
...@@ -1482,14 +1482,12 @@ module.exports = (function() { ...@@ -1482,14 +1482,12 @@ module.exports = (function() {
if (options.where) { if (options.where) {
for (var attr in options.where) { for (var attr in options.where) {
if (typeof attr === "string") {
if (Model.rawAttributes[attr] && Model.rawAttributes[attr].field) { if (Model.rawAttributes[attr] && Model.rawAttributes[attr].field) {
options.where[Model.rawAttributes[attr].field] = options.where[attr]; options.where[Model.rawAttributes[attr].field] = options.where[attr];
delete options.where[attr]; delete options.where[attr];
} }
} }
} }
}
return options; return options;
}; };
......
...@@ -148,7 +148,7 @@ describe(Support.getTestDialectTeaser("Model"), function () { ...@@ -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; var self = this;
return this.User.create({ return this.User.create({
...@@ -167,16 +167,16 @@ describe(Support.getTestDialectTeaser("Model"), function () { ...@@ -167,16 +167,16 @@ describe(Support.getTestDialectTeaser("Model"), function () {
{model: self.Comment}, {model: self.Comment},
{model: self.User} {model: self.User}
], ],
where: {title: 'DoDat'} where: {title: 'DatDo'}
}); });
}).then(function (task) { }).then(function (task) {
expect(task.get('title')).to.equal('DoDat'); expect(task.get('title')).to.equal('DatDo');
expect(task.get('comments'))[0].to.equal('Comment'); expect(task.get('comments')[0].get('text')).to.equal('Comment');
expect(task.get('user')).to.be.ok; 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; var self = this;
return this.User.create({ 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!