如何使用小马实体定义外键

发布于 2025-01-18 06:22:03 字数 366 浏览 3 评论 0原文

我想定义带有小马实体的外键。

如果我理解正确的话,只要命名为ID,就无需定义主键。因此,工作的主要关键是ID,我想定义与作业ID相关的食谱上的外键job_id。我尝试了必需(jobs.id),但这给出了类型错误。您对如何做有任何暗示吗?谢谢

class Jobs(db.Entity):
    path = Required(str)
    date = Required(str)

class Recipe(db.Entity):
    job_id = Required(int)    # must be foreign
    path = OptionalField(str)
    date = OptionalField(str)

I would like to define a foreign key with pony entities.

If I understand it right, there is no need to define a primary key as long as it is named id. so the primary key of Jobs is id, and I want to define a foreign key job_id on Recipe which is related to id of Jobs. I tried Required(Jobs.id) but this gives a type error. do you have any hint on how to do it? thank you

class Jobs(db.Entity):
    path = Required(str)
    date = Required(str)

class Recipe(db.Entity):
    job_id = Required(int)    # must be foreign
    path = OptionalField(str)
    date = OptionalField(str)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剧终人散尽 2025-01-25 06:22:03

我找到了答案。棘手的是,两种班级都必须定义关系

class Jobs(db.Entity):
    path = Required(str)
    date = Required(str)
    jobs = Set("Recipe")

class Recipe(db.Entity):
    job_id = Required(Jobs)    # must be foreign
    path = OptionalField(str)
    date = OptionalField(str)

I found the answer. the tricky thing is that the relationship had to be defined in both classes

class Jobs(db.Entity):
    path = Required(str)
    date = Required(str)
    jobs = Set("Recipe")

class Recipe(db.Entity):
    job_id = Required(Jobs)    # must be foreign
    path = OptionalField(str)
    date = OptionalField(str)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文