Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize, how to retrieve specific fields from joined tables

I have:

db.Animal.findAll({ attributes: ['id', 'name'], include: [db.Age] }).success(function(results) {
    console.log(JSON.stringify(results));
});

It gives me the fields ID and NAME from table Animal... but I also want to get specific fields from table Age and not all the fields (this is how it is working right now).

What can I do?

like image 305
arielmcm Avatar asked Aug 22 '14 00:08

arielmcm


1 Answers

You can specify attributes within the include array. Try rearranging like so:

db.Animal.findAll({ attributes: ['id', 'name'], 
  include: [{ model: db.Age, attributes: ['field1'] }]
})
.success(function(res){ console.log( JSON.stringify(res) ) })
like image 141
jtschoonhoven Avatar answered Oct 31 '22 05:10

jtschoonhoven