sequelize.js

What does 'separate' in sequelize mean?


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.


Solution

  • 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);