Sequelize 自引用表/模型不递归

发布于 2025-01-10 07:27:51 字数 2397 浏览 0 评论 0原文

假设我的 RDBMS 中有一个名为“family”的表:

id祖先_idfamily_member
1null曾曾祖父
21曾祖父
32祖父
43父亲
54儿子
65孙子

“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":

idancestor_idfamily_member
1nullgreat-great-grandfather
21great-grandfather
32grandfather
43father
54son
65grandson

"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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文