Skip to content

Array of values breaks top level column query for eagerly loaded model. Single value works. No value also does not work #10947

Open

Description

@irothenbaum

I'm not sure if this is related to other issues (i.e., #9099) about eagerly loaded models, but I consider this case worth mentioning because changing one of the top level conditions from an array of values to a single value resolves it. Seems odd.

What are you doing?

I have an array of candidate ids (ids of records that might be the one I want)
and I want to select a single one based on which of that short list has a matching record on
an associated table.

        var candidateIds = [123, 456, 789]
        Conversation.findOne({
            include: [
                {
                    model: ConversationEscalation,
                    as: 'ConversationEscalation',
                    required: true
                }
            ],
            where: {
                id: candidateIds,  // < -- Changing this to candidateIds[0] for example, does not reproduce the error
                '$ConversationEscalation.escalation_id$': 456
            }
        })

The above throws a MySQL exception reading, Unknown column: ConversationEscalation.escalation_id

If candidateIds is a single value (not an array), then it works properly.

To Reproduce
Steps to reproduce the behavior:

  1. Define models:
    // Conversation model
    const Conversation = sequelizeInstance.define('conversations', {
        id: {
            type: Sequelize.UUID,
            primaryKey: true,
            allowNull: false,
        }
    });

    const ConversationEscalation = sequelizeInstance.define('conversationEscalations', {
        conversation_id: {
            type: Sequelize.UUID,
            primaryKey: true,
            allowNull: false,
            references: {
                model: 'conversations',
                key: 'id'
            },
            onUpdate: 'cascade',
            onDelete: 'cascade'
        },

        escalation_id: {
            type: Sequelize.UUID,
            primaryKey: true,
            allowNull: false,
        },
    });

    // don't care about the escalations table for this example.
  1. Define the association:
Conversation.hasMany(ConversationEscalation, {as: 'ConversationEscalation', foreignKey: 'conversation_id'})
  1. Run the following
        var candidateIds = [123, 456, 789]
        Conversation.findOne({
            include: [
                {
                    model: ConversationEscalation,
                    as: 'ConversationEscalation',
                    required: true
                }
            ],
            where: {
                id: candidateIds,  // < -- This as an array generates a broken query, a single balue works
                '$ConversationEscalation.escalation_id$': 456
            }
        })
  1. See error

Unknown column 'ConversationEscalation.escalation_id' in 'where clause'

Here's the query it is generating:

SELECT `conversations`.*, 
       `ConversationEscalation`.`conversation_id` AS 
       `ConversationEscalation.conversation_id`, 
       `ConversationEscalation`.`escalation_id`   AS 
       `ConversationEscalation.escalation_id` 
FROM   (SELECT `conversations`.`id`, 
        FROM   `conversations` AS `conversations` 
        WHERE  `conversations`.`id` IN (123,456,789) 
               AND `conversationescalation`.`escalation_id` = 456 
               AND (SELECT `conversation_id` 
                    FROM   `conversationescalations` AS `ConversationEscalation` 
                    WHERE  ( `ConversationEscalation`.`conversation_id` = 
                           `conversations`.`id` ) 
                    LIMIT  1) IS NOT NULL 
        LIMIT  1) AS `conversations` 
       INNER JOIN `conversationescalations` AS `ConversationEscalation` 
               ON `conversations`.`id` = 
                  `ConversationEscalation`.`conversation_id`; 

What do you expect to happen?

I expected a result (or an empty set) to be returned

What is actually happening?

Throwing an error

Environment

Dialect: mysql
Dialect library version: 14.14
Database version: 5.7.26
Sequelize version: 5.7.4
Node Version: 8.10
OS: Ubuntu 16.04

Tested with latest release: Yes (5.8.6)

EDIT:
It should also be worth noting that if the id condition is removed entirely, it also does not work which seems extra strange.

To be clear (given the models and associations defined above):

let includeArray =  [{
    model: ConversationEscalation,
    as: 'ConversationEscalation',
    required: true
}]

Conversation.findOne({
    include: includeArray
    where: {
        id: ["array", "of", "values"], // notice an array of values
        '$ConversationEscalation.escalation_id$': 'someId'
    }
})
// ^^^ Throws an error

Conversation.findOne({
    include: includeArray
    where: {
        // notice no id condition
        '$ConversationEscalation.escalation_id$': 'someId'
    }
})
// ^^^^ Also throws an error

Conversation.findOne({
    include: includeArray
    where: {
        id: "someId", // notice a single (non array) value
        '$ConversationEscalation.escalation_id$': 'someId'
    }
})
// ^^^ Does not throw an error (works as expected)

Weird...

Activity

  1. changed the title [-]Array of values breaks top level column query for eagerly loaded model. Single value works[/-] [+]Array of values breaks top level column query for eagerly loaded model. Single value works. No value also does not work[/+] on May 14, 2019
  2. papb commented on Jul 25, 2019

    @papb
    Member

    Hello! What exactly were you expecting for when you pass an array of values? Since id is a number, I don't see why you would want to pass an array there. Perhaps you want to use an OR operator (Op.or)?

  3. added
    status: awaiting response
    on Jul 25, 2019
  4. irothenbaum commented on Jul 30, 2019

    @irothenbaum
    Author

    @papb
    Hey, sorry for the delay - had a busy last week.
    I'm expecting it to construct a id in [...] statement as it does when using that syntax without an include.

  5. added
    type: bug
    and removed
    status: awaiting response
    on Jul 30, 2019
  6. irothenbaum commented on Feb 11, 2020

    @irothenbaum
    Author

    Just adding some new information, this error only seems to happen when using findOne. If I swap it to a findAll it executes without error and seemingly correctly (except obviously returns an array instead of a single model).

    For anyone looking for a quick fix, there it is. Execute a findAll and just return the first element or null

  7. github-actions commented on Nov 10, 2021

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

  8. amiraiman commented on Dec 24, 2023

    @amiraiman

    @papb @ephys I faced this issue as well, and changing from findOne to findAll indeed removes the error. But unlike OP, im using a string as the value to query for instead of a string.

    After further experiment, this issue still persist even when using findAll with a limit options set in the query options. The only way to query based on nested association values is to use findAll without any limit.
    image

    Is there any reason why? By looking at the query generated when the query fails, apparently the where clauses was included in the subquery after the FROM statement
    image

    But when using findAll without any limit, the where clauses is placed at the end of the main query after joining all the tables
    image

  9. added theissue type on Mar 9, 2025
  10. removed
    type: bug
    on Mar 9, 2025
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

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions