Skip to content

The error message for a wrong alias could be better #10040

@papb

Description

@papb

Consider the code below, which shows a mistake in which the wrong alias is given:

const Sequelize = require('sequelize');
const sequelize = new Sequelize({ dialect: 'sqlite', storage: 'db.sqlite' });

const Foo = sequelize.define("foo", { name: Sequelize.STRING });
const Bar = sequelize.define("bar", { name: Sequelize.STRING });
Foo.hasMany(Bar, { as: "some_alias" });

sequelize.sync()
    .then(() => Foo.create({ name: "foo" }))
    .then(() => Bar.create({ name: "bar", fooId: 1 }))
    .then(() => Foo.findAll({
        include: {
            model: Bar,
            as: "wrong_alias"
        }
    }))
    .then(result => {
        console.log(JSON.stringify(result, null, 4));
    });

Sequelize throws the following error:

SequelizeEagerLoadingError: bar is associated to foo using an alias. You've included an alias (wrong_alias), but it does not match the alias defined in your association.

It is already great that it includes the wrong alias in the error message, but it would be even more helpful if the correct alias(es) were also given in the error message. Something like:

SequelizeEagerLoadingError: bar is associated to foo using an alias. You've included an alias (wrong_alias), but it does not match the alias(es) defined in your association (some_alias).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions