Skip to content

Incorrect Column Name when include, through & where are used together #6052

Closed

Description

@harshithkashyap

What you are doing?

var User = sequelize.define('User', {
    username: {
        type: Sequelize.STRING,
        primaryKey: true
    }
});

var Entity = sequelize.define('Entity', {
    entity_id: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    creator: {
        type: Sequelize.STRING,
        allowNull: false
    },
    votes: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 0
    }
});

var Post = sequelize.define('Post', {
    post_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        primaryKey: true
    }
});

var TaggableSentient = sequelize.define('TaggableSentient', {
    nametag: {
        type: Sequelize.STRING,
        primaryKey: true
    }
});

var EntityTag = sequelize.define('EntityTag', {
    entity_id: {
        type: Sequelize.INTEGER,
        allowNull: false
    },
    tag_name: {
        type: Sequelize.STRING,
        allowNull: false
    }
});

Entity.belongsTo(User, { foreignKey: 'creator', targetKey: 'username' });
Post.belongsTo(Entity, { foreignKey: 'post_id', targetKey: 'entity_id' });

Entity.belongsToMany(TaggableSentient, { 
    as: 'tags', 
    through: { model: 'EntityTag', unique: false }, 
    foreignKey: 'entity_id', 
    otherKey: 'tag_name' 
});

TaggableSentient.belongsToMany(Entity, { 
    as: 'tags', 
    through: { model: 'EntityTag', unique: false }, 
    foreignKey: 'tag_name', 
    otherKey: 'entity_id' 
});

sequelize.sync({ force: true, logging: console.log }).then(function() {
    Post.findAll({
        include: [{
            model: Entity,
            required: true,
            include: [{
                model: User,
                required: true
            },{
                model: TaggableSentient,
                as: 'tags',
                required: true,
                through: {
                    where: {
                        tag_name: ['tag1', 'tag2']
                    }
                }
            }]
        }],
        limit: 5,
        offset: 0
    }).then(function(posts) {
        console.log(posts)
    });
}).catch(function(err) {
    console.error(err);
});

What do you expect to happen?

The above findAll query is supposed to return all the posts where tags have been applied for the post and the tag_name is in specified list.

What is actually happening?

When generating the SQL query, sequelize is using incorrect column name and the same error is thrown. From the SQL query below, the where clause should be Post.post_id = Entity.entity_id but sequelize is generating Entity.post_id.

SELECT
  `Post`.*,
  `Entity.tags`.`nametag` AS `Entity.tags.nametag`,
  `Entity.tags`.`createdAt` AS `Entity.tags.createdAt`,
  `Entity.tags`.`updatedAt` AS `Entity.tags.updatedAt`,
  `Entity.tags.EntityTag`.`entity_id` AS `Entity.tags.EntityTag.entity_id`,
  `Entity.tags.EntityTag`.`tag_name` AS `Entity.tags.EntityTag.tag_name`,
  `Entity.tags.EntityTag`.`createdAt` AS `Entity.tags.EntityTag.createdAt`,
  `Entity.tags.EntityTag`.`updatedAt` AS `Entity.tags.EntityTag.updatedAt`
FROM
  (
  SELECT
    `Post`.`post_id`,
    `Post`.`createdAt`,
    `Post`.`updatedAt`,
    `Entity`.`entity_id` AS `Entity.entity_id`,
    `Entity`.`creator` AS `Entity.creator`,
    `Entity`.`votes` AS `Entity.votes`,
    `Entity`.`createdAt` AS `Entity.createdAt`,
    `Entity`.`updatedAt` AS `Entity.updatedAt`,
    `Entity.User`.`username` AS `Entity.User.username`,
    `Entity.User`.`createdAt` AS `Entity.User.createdAt`,
    `Entity.User`.`updatedAt` AS `Entity.User.updatedAt`
  FROM
    `Posts` AS `Post`
  INNER JOIN
    `Entities` AS `Entity` ON `Post`.`post_id` = `Entity`.`entity_id`
  INNER JOIN
    `Users` AS `Entity.User` ON `Entity`.`creator` = `Entity.User`.`username`
  WHERE
    (
    SELECT
      `Entity`.`entity_id`
    FROM
      `Entities` AS `Entity`
    INNER JOIN
      (
        `EntityTags` AS `tags.EntityTag`
      INNER JOIN
        `TaggableSentients` AS `tags` ON `tags`.`nametag` = `tags.EntityTag`.`tag_name` AND `tags.EntityTag`.`tag_name` IN('tag1',
        'tag2')
      ) ON `Entity`.`entity_id` = `tags.EntityTag`.`entity_id`
    WHERE
      `Post`.`post_id` = `Entity`.`post_id` #This line is using incorrect column name Entity.post_id
    LIMIT 1
  ) IS NOT NULL
ORDER BY
  `Entity.votes` DESC
LIMIT 0,
5
) AS `Post`
INNER JOIN
  (
    `EntityTags` AS `Entity.tags.EntityTag`
  INNER JOIN
    `TaggableSentients` AS `Entity.tags` ON `Entity.tags`.`nametag` = `Entity.tags.EntityTag`.`tag_name` AND `Entity.tags.EntityTag`.`tag_name` IN('tag1',
    'tag2')
  ) ON `Entity.entity_id` = `Entity.tags.EntityTag`.`entity_id`
ORDER BY
  `Entity.votes` DESC;

Dialect: any
Database version: mysql Ver 14.14 Distrib 5.5.49
Sequelize version: 3.23.3

From the query generator, i found that this line is causing the incorrect column name.
https://github.com/sequelize/sequelize/blob/v3/lib/dialects/abstract/query-generator.js#L1310

Using topInclude.model.primaryKeyAttributes[0] instead of topParent.model.primaryKeyAttributes[0] fixes the error.

Activity

  1. changed the title [-]Incorrect column name is used when using findAll with subQuery and INNER Join of included model(m:n)[/-] [+]Incorrect Column Name when using include + through + where[/+] on Nov 12, 2016
  2. changed the title [-]Incorrect Column Name when using include + through + where[/-] [+]Incorrect Column Name when using include + through + where is used[/+] on Nov 12, 2016
  3. changed the title [-]Incorrect Column Name when using include + through + where is used[/-] [+]Incorrect Column Name when include, through & where are used together[/+] on Nov 12, 2016
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions