Sequelize:设置单独的标志会导致未知列错误

时间:2019-10-14 13:05:29

标签: node.js sequelize.js

我的工作岗位模型是这样的:

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模型设置了 separate标志

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%');

由于我按预期使用了单独的位置,因此未执行加入。条件包含无效查询的地方。

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

如果我在jobApplication中的where子句中移动,则预期的输出将更改。如果firstName不匹配,则不需要职位发布。

如果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一样。

1 个答案:

答案 0 :(得分:0)

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

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,
            },
        ],
    });