Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

201
Visualizações
Select and join with sequelize

How to make this code with the sequelize?

I used include, but it brings all the user data but I only need the name

select comments.comment, users.name from comments inner join users on comments.userId = users.id

my code:

Comments.findAll({
       where: {
           projectId: id
       },
       include:  User
   }).then(response =>{
      
           res.json({comments: response})
       }) 
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you just want to reduce User model attributes then just indicate needed attributes in the attributes option:

Comments.findAll({
       where: {
           projectId: id
       },
       include:  [{ 
         model: User,
         attributes: ['name']
       }]
   }).then(response =>{
      
           res.json({comments: response})
       }) 

If you want to add a user's name as a string prop at the same level as all Comments fields simply indicate it in the attributes option of Comments model with a desired alias:

Comments.findAll({
       where: {
           projectId: id
       },
       attributes: [[Sequelize.col('Users.name'), 'userName']],
       include:  [{ 
         model: User,
         attributes: []
       }]
   }).then(response =>{
      
           res.json({comments: response})
       }) 
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda