how to define a foreign key with pony entities

181 Views Asked by At

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)
1

There are 1 best solutions below

0
On

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)