续集中的“分离"是什么意思? [英] What does 'separate' in sequelize mean?

查看:28
本文介绍了续集中的“分离"是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了 sequelize 的官方文档,但找不到任何关于separate"的条目.https://readthedocs.org/search/?q=separate

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
        }]
    })

为了找出它的含义,我将 'separate' 设置为 false,但查询的结果与我输入 '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.

推荐答案

我在 当前代码:

如果为 true,则运行单独的查询来获取关联的实例,仅支持 hasMany 关联

If true, runs a separate query to fetch the associated instances, only supported for hasMany associations

详细说明:默认情况下,要检索相关模型实例,Sequelize 将使用 SQL JOIN.通过启用 separate,Sequelize 将对每个关联的模型执行单独的查询,并在代码中连接结果文档(而不是让数据库执行连接).

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).

假设我有 Product 模型,其中 hasMany 关联到 Tag 模型(一个产品可以有许多与它").

Assume I have Product model with a hasMany association to the Tag model ("a product can have many tags associated with it").

这是常规"查询:

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`;

这里是 separate : true 查询:

SELECT 
  `product`.`id`,
  `product`.`title`
FROM `products` AS `product`;

SELECT
  `id`,
  `name`,
  `productId`
FROM `tags` AS `tag`
WHERE 
  `tag`.`productId` IN (1);

这篇关于续集中的“分离"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆