Sequelize 自引用表/模型不递归
假设我的 RDBMS 中有一个名为“family”的表:
id | 祖先_id | family_member |
---|---|---|
1 | null | 曾曾祖父 |
2 | 1 | 曾祖父 |
3 | 2 | 祖父 |
4 | 3 | 父亲 |
5 | 4 | 儿子 |
6 | 5 | 孙子 |
“id”是主键,自增字段,“ancestor_id”是一个外键,引用它自己的表,换句话说,“ancestor_id”是一个“family”的外键,目标列“id”。一个引用自身的表,对吗?到目前为止,没有什么新鲜事。为了更好地说明该示例,我添加了一个 varchar 列“family_member”,当人们可视化意图时,它将派上用场。
我使用 Sequelize 作为我的 ORM 解决方案。我不是这方面的初学者,也不是 ORM 技术的初学者。在注释出现之前,在 JPA 开始封装它之前(大约 17 年前),我就在使用 Hibernate。
当我查询家庭成员 id=6(孙子)时,我应该如何配置模型并将查询放在一起,以便 Sequelize 为我提供以下对象:
{
id: 6,
ancestor_id: 5,
family_member: "grandson"
ancestor: {
id: 5,
ancestor_id: 4,
family_member: "son"
ancestor: {
id: 4,
ancestor_id: 3,
family_member: "father"
ancestor: {
id: 3,
ancestor_id: 2,
family_member: "grandfather"
ancestor: {
id: 2,
ancestor_id: 1,
family_member: "great-grandfather"
ancestor: {
id: 1,
ancestor_id: null,
family_member: "great-great-grandfather"
}
}
}
}
}
}
无论我做什么,Sequelize 都不会“递归” ” 就这一点而言,我所能得到的只是一层深度,如果我查询“孙子”并要求在查询中包含模型本身,它只会返回“儿子”,而不会返回任何高于此的值。
我需要的是让 Sequelize 继续下去,直到找到 null“ancestor_id”,正如您在我的 JSON 示例中看到的那样。
感谢您的时间和耐心。
Lets suppose I have the following table in my RDBMS called "family":
id | ancestor_id | family_member |
---|---|---|
1 | null | great-great-grandfather |
2 | 1 | great-grandfather |
3 | 2 | grandfather |
4 | 3 | father |
5 | 4 | son |
6 | 5 | grandson |
"id" is the primary key and an auto-increment field, "ancestor_id" is a foreign key witch references it's own table, in other words, "ancestor_id" is a foreign key of "family", target column "id". A table that references itself, right? So far, nothing new. To better illustrate the example, I've added a varchar column, "family_member", it will come in handy latter as one visualizes the intend.
I'm using Sequelize as my ORM solution. I'm not a beginner in it, nor am I a beginner in ORM technology. I was using Hibernate before annotations were a thing, and before JPA started encapsulating it (some 17 years ago).
How should I configure the Model and put my query together, in order for Sequelize to give me the following object, as I query for family member id=6 (the grandson):
{
id: 6,
ancestor_id: 5,
family_member: "grandson"
ancestor: {
id: 5,
ancestor_id: 4,
family_member: "son"
ancestor: {
id: 4,
ancestor_id: 3,
family_member: "father"
ancestor: {
id: 3,
ancestor_id: 2,
family_member: "grandfather"
ancestor: {
id: 2,
ancestor_id: 1,
family_member: "great-grandfather"
ancestor: {
id: 1,
ancestor_id: null,
family_member: "great-great-grandfather"
}
}
}
}
}
}
No matter what I do, Sequelize doesn't go "recursive" on that one, all that I can get is one level deep, if I query for the "grandson" and ask to include the Model itself in the query, it will only return me the "son", and nothing above that.
What I need for this one is for Sequelize to go on until it finds a null "ancestor_id", as you can see in my JSON example.
Thanks for the time and patience.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论