【问题标题】:Sequelize : setting separate flag causes Unknown column errorSequelize:设置单独的标志会导致未知列错误
【发布时间】:2019-10-14 21:31:41
【问题描述】:

我的职位发布模型是这样的:

module.exports = (sequelize, DataTypes) => {
    const jobPost = sequelize.define('JobPost', {
        id: {
            type: DataTypes.BIGINT,
            allowNull: true,
            autoIncrement: true,
            primaryKey: true,
        },
        jobTitle: {
            type: DataTypes.STRING(150),
            allowNull: true,
        },
    }, {
        timestamps: true,
        paranoid: true,
    });

    jobPost.associate = (models) => {
        jobPost.hasMany(models.JobApplication);
    };
    return jobPost;
};

职位申请模型:

module.exports = (sequelize, DataTypes) => {
    const jobApplication = sequelize.define('JobApplication', {
        id: {
            type: DataTypes.BIGINT,
            allowNull: true,
            autoIncrement: true,
            primaryKey: true,
        },
        firstName: {
            type: DataTypes.STRING(150),
            allowNull: true,
        },
    }, {
        timestamps: true,
        paranoid:true,
    });
    jobApplication.associate = (models) => {
        jobApplication.belongsTo(models.Member, { as: 'applicant' });
        jobApplication.belongsTo(models.JobPost);
    };
    return jobApplication;
};

根据姓名查询职位的代码:

const jobPosts = await db.JobPost.findAll({
    attributes: ['id', 'jobTitle',],
    where: {
        '$JobApplications.firstName$': { 
            [db.Sequelize.Op.like]: `%${firstName}%`, 
        }
    },
    include: [
        {
            model: db.JobApplication,
            attributes: ['id', 'applicantId', 'firstName'],
        },
    ],
});

我得到了预期的输出:

"jobPosts": [
            {
                "id": 4,
                "jobTitle": "Quality Assurance Engineer II",
                "JobApplications": [
                    {
                        "id": 2,
                        "applicantId": 5,
                        "firstName": "Shanu"
                    }
                ]
            },
            {
                "id": 5,
                "jobTitle": "Senior QA Engineer/ QA Engineer",
                "JobApplications": [
                    {
                        "id": 3,
                        "applicantId": 6,
                        "firstName": "Anna"
                    }
                ]
            }
        ]

由于职位可以有多个职位申请,并且需要使用分页来提高性能,我为 jobApplication 模型设置了单独的标志。

const jobPosts = await db.JobPost.findAll({
        attributes: ['id', 'jobTitle',],
        where: {
            '$JobApplications.firstName$': { 
                [db.Sequelize.Op.like]: `%${firstName}%`, 
            }
        },
        include: [
            {
                model: db.JobApplication,
                attributes: ['id', 'applicantId', 'firstName'],
                separate:true,
            },
        ],
    });

我收到未知列错误:

Error: Unknown column 'JobApplications.firstName' in 'where clause'
       at Packet.asError (/home/nikhil/project/node_modules/mysql2/lib/packets/packet.js:684:17)
       at Query.execute (/home/nikhil/project/node_modules/mysql2/lib/commands/command.js:28:26)
       at Connection.handlePacket (/home/nikhil/project/node_modules/mysql2/lib/connection.js:455:32)
       at PacketParser.onPacket (/home/nikhil/project/node_modules/mysql2/lib/connection.js:73:18)
       at PacketParser.executeStart (/home/nikhil/project/node_modules/mysql2/lib/packet_parser.js:75:16)
       at Socket.<anonymous> (/home/nikhil/project/node_modules/mysql2/lib/connection.js:80:31)
       at Socket.emit (events.js:188:13)
       at addChunk (_stream_readable.js:288:12)
       at readableAddChunk (_stream_readable.js:269:11)
       at Socket.Readable.push (_stream_readable.js:224:10)
       at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:145:17)

Sql 查询输出是这样的:

SELECT `JobPost`.`id`, `JobPost`.`jobTitle` FROM `JobPosts` AS `JobPost` WHERE (`JobPost`.`deletedAt` IS NULL AND `JobApplications`.`firstName` LIKE '%n%');

没有执行加入,因为我按预期使用了单独的。 where 条件包含无效查询。

但是,解决办法是什么?如果我在没有单独标志的情况下运行,它可以工作,但是,它会破坏我的分页和性能。

如果我移动,jobApplication 中的 where 子句,预期的输出会发生变化。如果名字不匹配,我不需要职位发布。

如果 firstName = "Shanu" ,则输出为

{
        "jobPosts": [
            {
                "id": 4,
                "jobTitle": "Quality Assurance Engineer II",
                "JobApplications": [
                    {
                        "id": 2,
                        "applicantId": 5,
                        "firstName": "Shanu"
                    }
                ]
            },
            {
                "id": 5,
                "jobTitle": "Senior QA Engineer/ QA Engineer",
                "JobApplications": []
            }
        ]
    }

我不需要第二个对象。它的行为就像 JobApplication 的 required 设置为 false。

【问题讨论】:

    标签: node.js sequelize.js


    【解决方案1】:

    问题是您试图访问查询中未提供的列,因为它是分开的。当您将单独添加到包含的模型时,它看起来像这样:

    SELECT foo FROM bar WHERE bar.id IN (SELECT id FROM baz WHERE firstName LIKE 'some string');
    

    只需将 where 子句移动到包含的模型中,例如

    const jobPosts = await db.JobPost.findAll({
            attributes: ['id', 'jobTitle',],
            include: [
                {
                    model: db.JobApplication,
                    attributes: ['id', 'applicantId', 'firstName'],
                    where: {
                        firstName: {
                            [db.Sequelize.Op.like]: `%${firstName}%`
                        }
                    },
                    separate:true,
                },
            ],
        });
    

    【讨论】:

    • 感谢您的回复,但如果我移动 where 子句,我将获得没有 firName 条件的 JobPosts。请检查问题的最后一部分。正如我提到的,有什么方法可以获得预期的输出?
    猜你喜欢
    • 2017-09-21
    • 2011-06-05
    • 2019-10-04
    • 2016-07-05
    • 2015-12-16
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多