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

Commit 08ab29cd by Pedro Augusto de Paula Barbosa Committed by Sushant

fix(model): improve wrong alias error message (#10041)

1 parent f6d2b478
...@@ -698,8 +698,9 @@ class Model { ...@@ -698,8 +698,9 @@ class Model {
association = this.getAssociationForAlias(targetModel, targetAlias); association = this.getAssociationForAlias(targetModel, targetAlias);
if (!association) { if (!association) {
if (targetAlias) { if (targetAlias) {
const existingAliases = this.getAssociations(targetModel).map(association => association.as);
throw new sequelizeErrors.EagerLoadingError(`${targetModel.name} is associated to ${this.name} using an alias. ` + throw new sequelizeErrors.EagerLoadingError(`${targetModel.name} is associated to ${this.name} using an alias. ` +
`You've included an alias (${targetAlias}), but it does not match the alias defined in your association.`); `You've included an alias (${targetAlias}), but it does not match the alias(es) defined in your association (${existingAliases.join(', ')}).`);
} else { } else {
throw new sequelizeErrors.EagerLoadingError(`${targetModel.name} is associated to ${this.name} using an alias. ` + throw new sequelizeErrors.EagerLoadingError(`${targetModel.name} is associated to ${this.name} using an alias. ` +
'You must use the \'as\' keyword to specify the alias within your include statement.'); 'You must use the \'as\' keyword to specify the alias within your include statement.');
......
...@@ -646,7 +646,7 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -646,7 +646,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
const self = this; const self = this;
return self.Worker.findAll({ include: [{ model: self.Task, as: 'Work' }] }).catch (err => { return self.Worker.findAll({ include: [{ model: self.Task, as: 'Work' }] }).catch (err => {
expect(err.message).to.equal('Task is associated to Worker using an alias. ' + expect(err.message).to.equal('Task is associated to Worker using an alias. ' +
'You\'ve included an alias (Work), but it does not match the alias defined in your association.'); 'You\'ve included an alias (Work), but it does not match the alias(es) defined in your association (ToDo).');
}); });
}); });
...@@ -859,7 +859,7 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -859,7 +859,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
const self = this; const self = this;
return self.Worker.findAll({ include: [{ model: self.Task, as: 'Work' }] }).catch (err => { return self.Worker.findAll({ include: [{ model: self.Task, as: 'Work' }] }).catch (err => {
expect(err.message).to.equal('Task is associated to Worker using an alias. ' + expect(err.message).to.equal('Task is associated to Worker using an alias. ' +
'You\'ve included an alias (Work), but it does not match the alias defined in your association.'); 'You\'ve included an alias (Work), but it does not match the alias(es) defined in your association (ToDos).');
}); });
}); });
......
...@@ -568,7 +568,7 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -568,7 +568,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
it('throws an error indicating an incorrect alias was entered if an association and alias exist but the alias doesn\'t match', function() { it('throws an error indicating an incorrect alias was entered if an association and alias exist but the alias doesn\'t match', function() {
const self = this; const self = this;
return self.Worker.findOne({ include: [{ model: self.Task, as: 'Work' }] }).catch (err => { return self.Worker.findOne({ include: [{ model: self.Task, as: 'Work' }] }).catch (err => {
expect(err.message).to.equal('Task is associated to Worker using an alias. You\'ve included an alias (Work), but it does not match the alias defined in your association.'); expect(err.message).to.equal('Task is associated to Worker using an alias. You\'ve included an alias (Work), but it does not match the alias(es) defined in your association (ToDo).');
}); });
}); });
...@@ -729,7 +729,7 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -729,7 +729,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
it('throws an error indicating an incorrect alias was entered if an association and alias exist but the alias doesn\'t match', function() { it('throws an error indicating an incorrect alias was entered if an association and alias exist but the alias doesn\'t match', function() {
const self = this; const self = this;
return self.Worker.findOne({ include: [{ model: self.Task, as: 'Work' }] }).catch (err => { return self.Worker.findOne({ include: [{ model: self.Task, as: 'Work' }] }).catch (err => {
expect(err.message).to.equal('Task is associated to Worker using an alias. You\'ve included an alias (Work), but it does not match the alias defined in your association.'); expect(err.message).to.equal('Task is associated to Worker using an alias. You\'ve included an alias (Work), but it does not match the alias(es) defined in your association (ToDos).');
}); });
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!