Repository navigation
Description
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:
- 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.- Define the association:
Conversation.hasMany(ConversationEscalation, {as: 'ConversationEscalation', foreignKey: 'conversation_id'})- 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
}
})- 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...
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
[-]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[/+]papb commentedon Jul 25, 2019
Hello! What exactly were you expecting for when you pass an array of values? Since
idis 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)?irothenbaum commentedon Jul 30, 2019
@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.irothenbaum commentedon Feb 11, 2020
Just adding some new information, this error only seems to happen when using
findOne. If I swap it to afindAllit 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
findAlland just return the first element ornullgithub-actions commentedon Nov 10, 2021
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. 🙂
amiraiman commentedon Dec 24, 2023
@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.

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

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