Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use an include with attributes with sequelize?

Any idea how to use an include with attributes (when you need to include only specific fields of the included table) with sequelize?

Currently I have this (but it doesn't work as expected):

var attributes = ['id', 'name', 'bar.version', ['bar.last_modified', 'changed']]; foo.findAll({     where      : where,     attributes : attributes,     include    : [bar] }).success(function (result) { ... 
like image 901
borisdiakur Avatar asked Feb 19 '14 14:02

borisdiakur


People also ask

How do I use includes in Sequelize?

To wrap up, include takes an array of objects. These objects are queries of their own, essentially just Sequelize queries within our main query. Inside each include query we specify the associated model , narrow our results with where , and alias our returned rows with as .

How do you include two models in Sequelize?

Your solution, along with using include: {all:true} in my findAll query, did the trick. instead of using include: {all:true} in findAll you can use include: {model: models. User, as: 'createdByUser'} , etc. Be sure to use as: in all associations to the same model in your Sequelize model declaration.

How do I use Groupby Sequelize?

In Sequelize, you can add the group option in your query method findAll() to add the GROUP BY clause to the generated SQL query. Now you want to select all firstName values and group any duplicate values of the column. Here's the code for calling the findAll() method on the model: const users = await User.


2 Answers

Something like this should work

foo.findAll({     where      : where,     attributes : attributes,     include    : [{ model: bar, attributes: attributes}] }).success(function (result) { 
like image 167
Jan Aagaard Meier Avatar answered Sep 21 '22 13:09

Jan Aagaard Meier


We can do something like that for exclude or include specific attribute with sequelize in Node.js.

Payment.findAll({     where: {         DairyId: req.query.dairyid     },     attributes: {         exclude: ['createdAt', 'updatedAt']     },     include: {         model: Customer,         attributes:['customerName', 'phoneNumber']     } }) 
like image 24
Sanket Vanani Avatar answered Sep 19 '22 13:09

Sanket Vanani