I searched in the official docs of sequelize and couldn't find any entry about 'separate
'.https://readthedocs.org/search/?q=separate
I also searched on google but in vain.
db.fooTable.find({
where: {
id: id
},
include: [{
model: db.barTable1,
separate: true
}, {
model: db.barTable2,
separate: true
}, {
model: db.barTable3,
separate: true
}]
})
To find out what it means, I set 'separate
' to false, but the result of the query were the same as to when I put 'true
' instead.
Sequelize is a promise-based, Node. js ORM (Object-relational mapping) for Postgres, MySQL, MariaDB, SQLite and Microsoft SQL Server. It features solid transaction support, relations, eager and lazy loading, read replication, and more.
To create a One-To-One relationship, the hasOne and belongsTo associations are used together; To create a One-To-Many relationship, the hasMany and belongsTo associations are used together; To create a Many-To-Many relationship, two belongsToMany calls are used together.
Vanilla Include Sequelize is smart enough to pull in any rows from your Artists model that are associated with your Albums. Include takes an array of objects. These objects have properties like model , as , and where which tell Sequelize how to look for associated rows.
There are two ways you can create JOIN queries and fetch data from multiple tables with Sequelize: Create raw SQL query using sequelize. query() method. Associate related Sequelize models and add the include option in your Sequelize query method.
I found this in the current code:
If true, runs a separate query to fetch the associated instances, only supported for hasMany associations
To elaborate: by default, to retrieve the related model instance, Sequelize will use a SQL JOIN. By enabling separate
, Sequelize will perform a separate query for each of the associated models, and join the resulting documents in code (instead of letting the database perform the join).
Assume I have Product
model with a hasMany
association to the Tag
model ("a product can have many tags associated with it").
Here's the "regular" query:
SELECT
`product`.`id`,
`product`.`title`,
`tags`.`id` AS `tags.id`,
`tags`.`name` AS `tags.name`,
`tags`.`productId` AS `tags.productId`
FROM `products` AS `product`
LEFT OUTER JOIN `tags` AS `tags`
ON
`product`.`id` = `tags`.`productId`;
Here are the separate : true
queries:
SELECT
`product`.`id`,
`product`.`title`
FROM `products` AS `product`;
SELECT
`id`,
`name`,
`productId`
FROM `tags` AS `tag`
WHERE
`tag`.`productId` IN (1);
Additional to @robertklep response:
As you know now it separates an otherwise joined query.
This means that it would be more performant in some situations where you have many joins and nested joins (can have a huge impact in some). Nested joins make Sequelize take more time in a single big query than running multiple small ones. The problem is pointed out as a deduplication operation:
See here: Slow associations in SequelizeJS
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