sequelize: DatabaseError: Column does not exist when querying with nested include and where
What you are doing?
Post a minimal code sample that reproduces the issue, including models and associations
const Mail = sequelize.define('Mail', {
subject: Sequelize.TEXT,
content: Sequelize.TEXT
}, {
paranoid: true
});
const MailboxEntry = sequelize.define('MailboxEntry', {
movedToTrashAt: {
type: Sequelize.DATE,
allowNull: true
},
read: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false
}
}, {
paranoid: true
});
const User = sequelize.define('User', {
username: {
type: Sequelize.TEXT,
unique: true,
allowNull: false,
},
firstName: {
type: Sequelize.TEXT,
allowNull: false
},
lastName: {
type: Sequelize.TEXT,
allowNull: false
}
})
Mail.belongsTo(User, {
as: 'sender',
foreignKey: {
name: 'senderId',
allowNull: false
}
});
Mail.belongsToMany(User, {
as: 'recipients',
through: 'MailRecipients',
otherKey: {
name: 'recipientId',
allowNull: false
},
foreignKey: {
name: 'mailId',
allowNull: false
}
});
Mail.hasMany(MailboxEntry, {
as: 'mailboxEntries',
foreignKey: {
name: 'mailId',
allowNull: false
}
});
MailboxEntry.belongsTo(User, {
as: 'owner',
foreignKey: {
name: 'ownerId',
allowNull: false
}
});
MailboxEntry.belongsTo(Mail, {
as: 'mail',
foreignKey: {
name: 'mailId',
allowNull: false
}
});
const userId = 1;
sequelize.sync().then(() => MailboxEntry.findAndCount({
offset: 0,
limit: 10,
order: [['createdAt', 'DESC']],
include: [
{
association: MailboxEntry.associations.mail,
attributes: { exclude: ['content'] },
include: [
{
association: Mail.associations.recipients,
attributes: ['id', 'username', 'firstName', 'lastName'],
through: {
where: {
recipientId: userId
}
},
required: true
}
],
required: true
}
],
where: {
ownerId: userId,
movedToTrashAt: null
},
attributes: { exclude: ['movedToTrashAt'] }
}).then(result => console.log(result), err => console.error(err));
What do you expect to happen?
Getting back a list of mailbox entries with included mail (content) with included recipients, that belong to a user (ownerId), where the user is the recipient and that are not moved to trash.
What is actually happening?
SequelizeDatabaseError: Column Mail.mailId does not exist
Executed query:
SELECT "MailboxEntry".*,
"mail.recipients"."id" AS "mail.recipients.id",
"mail.recipients"."username" AS "mail.recipients.username",
"mail.recipients"."firstName" AS "mail.recipients.firstName",
"mail.recipients"."lastName" AS "mail.recipients.lastName",
"mail.recipients.MailRecipients"."createdAt" AS "mail.recipients.MailRecipients.createdAt",
"mail.recipients.MailRecipients"."updatedAt" AS "mail.recipients.MailRecipients.updatedAt",
"mail.recipients.MailRecipients"."mailId" AS "mail.recipients.MailRecipients.mailId",
"mail.recipients.MailRecipients"."recipientId" AS "mail.recipients.MailRecipients.recipientId"
FROM
(SELECT "MailboxEntry"."id",
"MailboxEntry"."read",
"MailboxEntry"."createdAt",
"MailboxEntry"."updatedAt",
"MailboxEntry"."deletedAt",
"MailboxEntry"."ownerId",
"MailboxEntry"."mailId",
"mail"."id" AS "mail.id",
"mail"."subject" AS "mail.subject",
"mail"."createdAt" AS "mail.createdAt",
"mail"."updatedAt" AS "mail.updatedAt",
"mail"."deletedAt" AS "mail.deletedAt",
"mail"."senderId" AS "mail.senderId"
FROM "MailboxEntries" AS "MailboxEntry"
INNER JOIN "Mails" AS "mail" ON "MailboxEntry"."mailId" = "mail"."id"
AND "mail"."deletedAt" IS NULL
WHERE ("MailboxEntry"."deletedAt" IS NULL
AND ("MailboxEntry"."ownerId" = '1'
AND "MailboxEntry"."movedToTrashAt" IS NULL))
AND
(SELECT "Mail"."id"
FROM "Mails" AS "Mail"
INNER JOIN ( "MailRecipients" AS "recipients.MailRecipients"
INNER JOIN "Users" AS "recipients" ON "recipients"."id" = "recipients.MailRecipients"."recipientId"
AND "recipients.MailRecipients"."recipientId" = '1') ON "Mail"."id" = "recipients.MailRecipients"."mailId"
AND "recipients"."deletedAt" IS NULL
WHERE "MailboxEntry"."id" = "Mail"."mailId" LIMIT 1) IS NOT NULL
ORDER BY "MailboxEntry"."createdAt" DESC LIMIT 10
OFFSET 0) AS "MailboxEntry"
INNER JOIN ("MailRecipients" AS "mail.recipients.MailRecipients"
INNER JOIN "Users" AS "mail.recipients" ON "mail.recipients"."id" = "mail.recipients.MailRecipients"."recipientId"
AND "mail.recipients.MailRecipients"."recipientId" = '1') ON "mail.id" = "mail.recipients.MailRecipients"."mailId"
AND "mail.recipients"."deletedAt" IS NULL
ORDER BY "MailboxEntry"."createdAt" DESC;
Dialect: postgres Database version: 9.5.2 Sequelize version: 3.23.3
About this issue
- Original URL
- State: open
- Created 8 years ago
- Reactions: 15
- Comments: 26 (8 by maintainers)
The same problem guys!
I am still having this issue on 5.5.0, any workaround?
I still have the issue in v4.38… @felixfbecker how is it possible? Or am I doing something wrong?
My association:
My query :
I get :
... LEFT OUTER JOIN Roles AS role ON User.id = role.roleId WHERE User.id = '1';"
It’s suppose to be :
... LEFT OUTER JOIN Roles AS role ON User.roleId = role.id WHERE User.id = '1';"
Hi! Same problem, v6.6.4. Any news?
Same issue
same issue on v5 on my localhost working wothout problem but on server have problem my db version on localhost and on my server is same
When defining the search without specifying the attributes to be returned, the error is shown:
await User.findAll({ where: { provider: true }, attributes: ['id', 'name', 'email'], include: [ { model: File, as: 'avatar', }, ], });
Error:Executing (default): SELECT "... ."updated_at" AS "avatar.updatedAt" FROM "users" AS "User" LEFT OUTER JOIN "files" AS "avatar" ON "User"."avatar_id" = "avatar"."id" WHERE "User"."provider" = true; (node:19224) UnhandledPromiseRejectionWarning: SequelizeDatabaseError: column avatar.email does not exist
When I defined the fields to be returned, no more errors are displayedinclude: [ { model: File, as: 'avatar', attributes: ['name', 'path'], }, ],
@mickhansen help please 🆘
Problem seems to be in
lib/dialects/abstract/query-generator.js
around line1285
. Only happens for me when usinglimit
with a nested include where. Sequelize is assuming the top-level include is ahasMany
association, when in fact it is abelongsTo
association, hence why it is “swapping” the primary/foreign key WHERE clause.