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

200
Views
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 answers
Answer question

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 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!