Question

I have three tables contractors, projects and jointable for these two is projects_contractors and i created models and wrote a relation like below,

    Contractor.hasMany(Project, {joinTableName: 'projects_contractors'})
    Project.hasMany(Contractor, {joinTableName: 'projects_contractors'})

I want to access this Contractor based projects means inner JOIN.

Core query : select c.id,c.name,p.id,p.name from contractors c inner join projects_contractors pc on c.id=pc.contractor_id inner join projects p on p.id = pc.project_id

I was failed in implementing the below code. "required" is a keyword which used for inner JOIN but not working if we keep.

     Contractor.findAll({ include: [Project, {required: false}]}).success(function(list){
       console.log("hi")
         res.send(204)
     })

If not keeping required it will creates a left outer JOIN on projects and contractors. Suggest me with a sample example for the above senario.

Was it helpful?

Solution

The correct syntax is:

Contractor.findAll({ include: [{model: Project, required: true}]})

Generally the include params take either a model, or an object with a model parameter and optional as/required/include/where params.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top