Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize in NodeJS : Inner JOIN implementation Failure

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.

like image 336
Ayyappa A Avatar asked Apr 29 '14 09:04

Ayyappa A


1 Answers

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.

like image 129
Mick Hansen Avatar answered Nov 13 '22 06:11

Mick Hansen