Skip to content

Query w/ multiple one to many includes fails w/ limit #8360

Closed

Description

@robert-armstrong

What are you doing?

To run:

npm install sequelize
npm install sqlite3
'use strict';

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

const Product = sequelize.define('products', {
  id: {
    type: Sequelize.INTEGER,
    primaryKey: true
  },
  name: {
    type: Sequelize.STRING
  }
});

const Tag = sequelize.define('tags', {
  id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
  },
  name: {
      type: Sequelize.STRING,
  }
});

const ProductTag = sequelize.define('products_tags', {
}, {
  createdAt: false,
  updatedAt: false
} );

var ProductAttribute = sequelize.define('product_attributes', {
  product_id: {
    type: Sequelize.INTEGER,
    primaryKey: true
  },
  value: {
    type: Sequelize.STRING
  }
});

Product.hasMany(ProductAttribute, { foreignKey: 'product_id', sourceKey: 'id' });
ProductAttribute.belongsTo(Product, { foreignKey: 'id' });

Product.belongsToMany(Tag, { through: ProductTag, foreignKey: 'product_id' });
Tag.belongsToMany(Product, { through: ProductTag, foreignKey: 'tag_id' });

sequelize.sync({ force: true }).then(() => {
  Product.findAll({
    offset: 0,
    limit: 100,
    include:
     [ { model: Tag,
         where: { name: { in: [ 'red' ] } },
         duplicating: false },
       { model: ProductAttribute } ],
    })
    .then(results => {
       console.log(results);
    });
});

What do you expect to happen?

Return the top 100 Product records that match the Tag equal to 'red', and all ProductAttributes associated with those Products.

What is actually happening?

Unhandled rejection SequelizeDatabaseError: SQLITE_ERROR: no such column: tags->products_tags.product_id
    at Query.formatError (/Users/robert/node_modules/sequelize/lib/dialects/sqlite/query.js:418:16)
    at afterExecute (/Users/robert/node_modules/sequelize/lib/dialects/sqlite/query.js:119:32)
    at replacement (/Users/robert/node_modules/sqlite3/lib/trace.js:19:31)
    at Statement.errBack (/Users/robert/node_modules/sqlite3/lib/sqlite3.js:16:21)

Query works if the limit and offset are removed. Query also works if only one include is used.

Dialect: mysql / sqlite
Dialect version: MySQL 5.5 and SQLite3
Sequelize version: 4.10.0
Tested with master branch: Yes

Note : Your issue may be ignored by maintainers if its not tested against master branch OR does not follow issue template.

Activity

  1. robert-armstrong commented on Sep 22, 2017

    @robert-armstrong
    Author

    Discovered I can work around this by using separate: true

    { model: ProductAttribute, separate: true }
    
  2. pauldps commented on Sep 26, 2017

    @pauldps

    separate: true also seems to fix issues with nested associations in queries with limit/order.

  3. stale commented on Feb 4, 2018

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions