我的工作岗位模型是这样的:
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%');
由于我按预期单独使用,因此未执行连接。其中条件包含无效查询。
但是,解决方案是什么?如果我在没有单独标志的情况下运行,它可以工作,但是,它会破坏我的分页和性能。
如果我移动,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。
最佳答案
问题是您正在尝试访问查询中未提供的列,因为它是分开的。当您将单独添加到包含的模型时,它看起来像这样:
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,
},
],
});
关于node.js - Sequelize : setting separate flag causes Unknown column error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58377505/