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) { ...
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 .
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.
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.
Something like this should work
foo.findAll({ where : where, attributes : attributes, include : [{ model: bar, attributes: attributes}] }).success(function (result) {
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'] } })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With