简体   繁体   English

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

[英]how to define a foreign key with pony entities

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.如果我没理解错的话,不用定义主键只要命名为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.所以Jobs的主键是id,我想在Recipe上定义一个与Jobs的id相关的外键job_id。 I tried Required(Jobs.id) but this gives a type error.我试过 Required(Jobs.id) 但这给出了类型错误。 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)

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM