Skip to content

findAll nested query produces error #3319

Closed

Description

@waltonseymour

When using the following options for findAll,

var options = {attributes: ['id', 'title', 'description', 'createdAt', 'price'], order: [['createdAt', 'DESC']], include: [{model: models.User, as: 'user', attributes: userOptions.attributes},
      {model: models.Photo, as: 'photos'},
      {model: models.Category, as: 'category', attributes: categoryOptions.attributes}]});

the query below is produced.

SELECT "Post"."id"
    ,"Post"."title"
    ,"Post"."description"
    ,"Post"."createdAt"
    ,"Post"."price"
    ,"user"."id" AS "user.id"
    ,"user"."email" AS "user.email"
    ,"photos"."id" AS "photos.id"
    ,"photos"."post_id" AS "photos.post_id"
    ,"photos"."createdAt" AS "photos.createdAt"
    ,"photos"."updatedAt" AS "photos.updatedAt"
    ,"category"."id" AS "category.id"
    ,"category"."name" AS "category.name"
FROM "posts" AS "Post"
LEFT JOIN "users" AS "user" ON "Post"."user_id" = "user"."id"
LEFT JOIN "photos" AS "photos" ON "Post"."id" = "photos"."post_id"
LEFT JOIN "categories" AS "category" ON "Post"."category_id" = "category"."id"
ORDER BY "Post"."createdAt" DESC;

However, when I add limit:10 to the options, this query is produced instead.

SELECT "Post".*
    ,"user"."id" AS "user.id"
    ,"user"."email" AS "user.email"
    ,"photos"."id" AS "photos.id"
    ,"photos"."post_id" AS "photos.post_id"
    ,"photos"."createdAt" AS "photos.createdAt"
    ,"photos"."updatedAt" AS "photos.updatedAt"
    ,"category"."id" AS "category.id"
    ,"category"."name" AS "category.name"
FROM (
    SELECT "Post"."id"
        ,"Post"."title"
        ,"Post"."description"
        ,"Post"."createdAt"
        ,"Post"."price"
    FROM "posts" AS "Post"
    ORDER BY "Post"."createdAt" DESC LIMIT 10
    ) AS "Post"
LEFT JOIN "users" AS "user" ON "Post"."user_id" = "user"."id"
LEFT JOIN "photos" AS "photos" ON "Post"."id" = "photos"."post_id"
LEFT JOIN "categories" AS "category" ON "Post"."category_id" = "category"."id"
ORDER BY "Post"."createdAt" DESC;

This produces an sql error as the innermost select will not contain any of the foreign keys referenced in the joins.

Is there any way to select all columns in the inner query, and then trim in the outer?

Activity

  1. mickhansen commented on Mar 11, 2015

    @mickhansen
    Contributor

    What version of sequelize? I believe we've covered most of these issues by now.
    Would be helpfull to see your model definitions and relation setups.

  2. waltonseymour commented on Mar 11, 2015

    @waltonseymour
    Author

    I am using sequelize 2.0.0

    Here's the Post model definition. Let me know if you need the others.

    /* jshint indent: 2 */
    
    module.exports = function(sequelize, DataTypes) {
      return sequelize.define('Post', {
        id: {
          type: DataTypes.UUID,
          allowNull: false,
          primaryKey: true,
          defaultValue: DataTypes.UUIDV4
        },
        user_id: {
          type: 'UUID',
          allowNull: false
        },
        description: {
          type: DataTypes.TEXT,
          allowNull: true
        },
        category_id: {
          type: 'UUID',
          allowNull: false
        },
        price: {
          type: DataTypes.INTEGER,
          allowNull: true
        },
        title: {
          type: DataTypes.STRING,
          allowNull: false
        },
        latitude: {
          type: DataTypes.FLOAT,
          allowNull: false
        },
        longitude: {
          type: DataTypes.FLOAT,
          allowNull: false
        }
      },
      {
        tableName: 'posts'
      });
    };

    The associations are created by calling:

    models.Post.belongsTo(models.User, {as: 'user', foreignKey: {name: 'user_id', allowNull: false}, onDelete: 'cascade'});
    models.Post.belongsTo(models.Category, {as: 'category', foreignKey: {name: 'category_id', allowNull: false}, onDelete: 'cascade'});
    models.Post.hasMany(models.Photo, {as: 'photos', foreignKey: 'post_id', onDelete: 'cascade'});
  3. mickhansen commented on Mar 12, 2015

    @mickhansen
    Contributor

    Have you tried the latest version?

  4. waltonseymour commented on Mar 12, 2015

    @waltonseymour
    Author

    Just updated to 2.0.4. Same issue persists.

  5. added
    type: bug
    on Mar 12, 2015
  6. mickhansen commented on Mar 12, 2015

    @mickhansen
    Contributor

    Looks like a bug then.

  7. mickhansen commented on Mar 30, 2015

    @mickhansen
    Contributor

    @bobdye there's generally poor support for aggregates and includes at the moment.

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

    staletype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions