I have problem to find and filter data from hasMany associations fields, am using following packages version: "sequelize": "^6.9.0", "mysql2": "^2.3.3-rc.0"
This is my tables:
const RequestForm = sequelize.define('RequestForm', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
allowNull: false,
noUpdate : true
},
type: DataTypes.STRING
})
const RequestStatus = sequelize.define('RequestStatus', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
allowNull: false,
noUpdate : true
},
})
const Employee = sequelize.define('Employee', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
allowNull: false,
noUpdate : true
}})
This is associations:
RequestStatus.belongsTo(RequestForm);
RequestForm.hasMany(RequestStatus);
RequestStatus.belongsTo(Employee);
Employee.hasMany(RequestStatus);
I am trying to find RequestForm data that have one of following condition:
and i want return all RequestStatus of RequestForm, so i can't but where in include
This My code but not working and get error -> original: Error: Unknown column 'RequestStatus.EmployeeId' in 'where clause'
RequestForm.find({
where: {
[Op.or]: [
{type: {[Op.in]: ["One", "Two"]}},
{
employee_id_value: Sequelize.where(
Sequelize.col("RequestStatuses.EmployeeId"),
{
[Op.eq]: "123e4567-e89b-12d3-a456-426614174000",
}
)
},
]
},
include:[
{
model: RequestStatus,
attributes: ['id', 'EmployeeId'],
required: false,
},
{Other Models}
]
})
You misspelled RequestStatus in Sequelize.col and you don't need to indicate employee_id_value field, use Sequelize.where directly:
where: {
[Op.or]: [
{
type: {
[Op.in]: ["One", "Two"]
}
},
Sequelize.where(Sequelize.col("RequestStatus.EmployeeId"),
Op.eq, "123e4567-e89b-12d3-a456-426614174000")
]
},