question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DatabaseError: Column does not exist when querying with nested include and where

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:13
  • Comments:24 (8 by maintainers)

github_iconTop GitHub Comments

8reactions
evilrocket216commented, Apr 8, 2020

The same problem guys!

8reactions
vivekhabilelabscommented, Aug 19, 2019

I am still having this issue on 5.5.0, any workaround?

Read more comments on GitHub >

github_iconTop Results From Across the Web

SequelizeDatabaseError: column does not exist (Postgresql)
When we write Manufacturer.hasMany(models.ManufacturerTab) and we query using include , sequelize will assume that ManufacturerTab contains ...
Read more >
[Solved]-sequelize raw query column doesn't exist-postgresql
Query to select data from database between 2 timestamps, Exception: time_bucket function does not exists? What happens when doing an upsert and has...
Read more >
Looker error catalog | Google Cloud
The Location column indicates where in Looker the error message is displayed, and includes the following options (some errors can appear in more...
Read more >
SQL error messages and exceptions - Oracle Help Center
Query does not qualify to generate an updatable ResultSet. ... 42909, The CREATE TABLE statement does not include a column list.
Read more >
Sequelize - How to fix relation does not exist error
In the case above, the error happens because Sequelize is trying to find Users table with an s , while the existing table...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found