I have the following basic SQLAlchemy model with a many to many self reference:
class Organisation(Base):
__tablename__ = 'organisation'
name = Column(String(50))
url = Column(String(128))
associated = relationship(
'Organisation',
secondary="organisation_map",
primaryjoin="Organisation.id==organisation_map.c.organisation_id",
secondaryjoin="Organisation.id==organisation_map.c.assoc_organisation_id",
backref="account_organisation"
)
The association table looks like this:
class OrganisationMap(Base):
__tablename__ = 'organisation_map'
organisation_id = Column(Integer, ForeignKey('organisation.id'))
assoc_organisation_id = Column(Integer, ForeignKey('organisation.id'))
is_customer = Column(Boolean, default=False)
is_supplier = Column(Boolean, default=False)
The association table contains extra data, is_customer
and is_supplier
that I want to be able to reference from the model itself, eg:
class Organisation(Base):
...
def get_suppliers(self):
pass
def get_customers(self):
pass
At the moment, I have no way of getting such a list without first querying the association table, OrganisationMap
, getting the ids of 'customers' or 'suppliers' and then querying the Organisation
table with a list of ids.
Is this possible?