SQLAlchemy 2.0 foreign key across parent and child tables

  Kiến thức lập trình

Say I have these tables which define hierarchy

Org

  • id
  • name

SubOrg

  • id
  • org_id
  • name

Now there is another table which refers both such that:

Rules

  • id
  • org_id
  • sub_org_id (nullable)

The way Rules table works is if sub_org_id is null, the rule applies to whole org. if sub_ord_id is not null, that rule overrides for that particular sub_org.

How do I map this into sqlalchemy relationship?

Thanks

LEAVE A COMMENT