Skip to content

[Bug] Nested includes with scopes & field names SQL errors #4210

@ackerdev

Description

@ackerdev

When updating from v3.3.0 to v3.5.1, I found that several queries were no longer working as intended, now throwing out SQL Errors. After doing a bit of bisecting, I found the source of the problem to be commit 538a095.

Here is my best estimation at providing a replicable test case, let me know if my pseudo doesn't recreate the issue on your side!

var Foo = sequelize.define('Foo');
var Bar = sequelize.define('Bar');
var Baz = sequelize.define('Baz', {
  qux: {
    type: Sequelize.INTEGER,
    field: 'quxId'
  }
}, {
  scopes: {
    quxScope: {
      where: {
        qux: 1
      }
    }
  }
});

Foo.belongsToMany(Bar);
Bar.belongsToMany(Foo);
Bar.hasMany(Baz);
Baz.belongsTo(Bar);

Foo.find({
  include: [{
    model: Bar,
    include: Baz.scope('quxScope')
  }]
});

Which produces an error along the lines of:

Unhandled rejection SequelizeDatabaseError: Invalid column name 'qux'.

In my case, it appears that Sequelize tried to alias the column like [Bar.Baz.quxId] AS [Bar.Baz.qux] but in the outer WHERE clause it attempts to add the quxScope and references the column as [Bar.Baz].[qux].

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