[英]Creating With a more complex association structure in sequelize
I am going off this small example off the sequelize docs我将从 sequelize 文档中删除这个小示例
https://sequelize.org/docs/v6/advanced-association-concepts/creating-with-associations/#belongsto--hasmany--hasone-association https://sequelize.org/docs/v6/advanced-association-concepts/creating-with-associations/#belongsto--hasmany--hasone-association
and I am not 100% following, for our project, we have a table that has a one to many association, but also two, one to many, associations within a one to many (I know a bit hard to word that)我不是 100% 关注,对于我们的项目,我们有一个表,它有一个一对多的关联,但也有两个,一对多,一对多内的关联(我知道有点难以表达)
so Table 1 has a one to many to table 2 and table 2 has a one to many to table 3 and a one to many to table 4所以表 1 与表 2 一对多,表 2 与表 3 一对多,表 4 一对多
and I am trying to understand how to translate that to the example in that site (url) above into something workable for myself.我正在尝试了解如何将上面那个站点 (url) 中的示例转换为对我自己可行的东西。
I just dont fully understand how to do it.我只是不完全明白该怎么做。
for example, looking at the example,例如,看这个例子,
include: [{
association: Product.User,
include: [ User.Addresses ]
}]
I am not sure what include means vs association means.我不确定 include 和 association 是什么意思。 Other examples on that page seem to use association in the same way as where include is used (I think).该页面上的其他示例似乎使用关联的方式与使用 include 的方式相同(我认为)。 I also am not sure how this works with multiple layers as I described above vs this more simple example.我也不确定这如何与我上面描述的多层一起工作,而不是这个更简单的例子。
Now I know this is a bigger-ish type of create but we are wondering if its a pluasble thing to do vs multiple calls to create data for those other tables when creating a largr dataset vs just doing a single create like this exmaple says is possible:)现在我知道这是一种更大的创建类型,但我们想知道在创建大型数据集时与多次调用为其他表创建数据相比,与像这个例子所说的那样只进行一次创建是可能的,这是否是一件可行的事情:)
if anyone has any advice I would aapperciate it, thanks!如果有人有任何建议,我会很感激,谢谢!
association
option is just another alternative to indicate an associated model: you either use include
and indicate both a model ans an alias OR use association
and indicate an association which already stores an associated model and its alias. association
选项只是指示关联 model 的另一种选择:您要么使用include
并指示 model 和别名,要么使用association
并指示已经存储关联 model 及其别名的关联。
As for 4 tables each of which in its turn is linked as one-to-many to the next one you just need to use nested include/association
options and that's all.至于 4 个表,每个表又以一对多的形式链接到下一个表,您只需要使用嵌套include/association
选项就可以了。 Also if you want to get all-in-one then don't forget to indicate separate: true
on each level of include/association
options otherwise a generated SQL query might have too many records and you'll get the out of memory error.此外,如果您想获得一体式功能,请不要忘记在每个级别的include/association
选项上指示separate: true
,否则生成的 SQL 查询可能包含太多记录,您将得到 out of memory 错误。
const allInOneItems = Table1.findAll({
include: [{
model: Table2,
separate: true,
include: [{
model: Table3,
separate: true,
include: [{
model: Table4,
separate: true,
}]
}]
}]
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.