Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
Sequelize: filter table by hassMany associations fields

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:

  1. RequestForm type = "One"
  2. RequestForm type = "Two"
  3. one of RequestStatus EmployeeId(FK) equal to "123e4567-e89b-12d3-a456-426614174000"

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}
]
})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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")
 ]
},
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!