-1
I am trying to query the sequelize but I am not succeeding. I need to search for a collaborator in the bank by license plate. However, the employee must be from the same sector as the manager logged in to the system. The manager’s setorId is req.user.id id
So is my code
await Colaborador.findOne({
include: [{
model: Cargo,
},
{
model: Setor,
where:{
setorId: req.user.setorId
}
}
]
},{
where:{
matricula: matriculaId
}
})
I also need to do an Inner Join with the position and sector tables to appear on the screen. The sequelize is not returning me this wanted employee. What would be my mistake?
I managed to do by pure SQL, but I would like to do by Sequelize same.
With pure sql gets like this:
let colaborador = await sequelize.query(`SELECT c.colaboradorId, c.nome as nomeColaborador, g.nome as nomeCargo ,s.nome as nomeSetor FROM colaborador c, setor s, cargo g WHERE c.setorId = s.setorId AND c.cargoId = g.cargoId AND c.setorId = ${req.user.setorId} AND c.matricula = ${matriculaId} LIMIT 1`, { type: QueryTypes.SELECT });
Thank you very much! It worked great!!! Now another question has arisen, what if the field of Where is related to a table of Include? setorId is in the collaborator table, now imagine that I need an attribute of the Position table that is in include and put a condition?
– Vitor Cordeiro
I can’t say for sure now because I never did, but there is a similar example in Soen, see here. The example in question is about an attribute of another table (B) with a fixed value (and not comparing with a value of table A). I believe that this is a pertinent question and can become another issue here in the SO, if there is no longer
– Rafael Tavares
I found that example also. I think it would suit your case, I’m not sure
– Rafael Tavares