I suggest you use inheritance. This is very, very well explained in the SqlAlchemy docs here and here.
My recommendation is to create an Anesthesia class and inherit from it both InhalationAnesthesia and TwoStepInjectionAnesthesia . This is your call to decide which type of table inheritance to use:
- single table inheritance
- White table inheritance
- attached table inheritance
The most common forms of inheritance are a single and combined table, while specific inheritance creates more configuration problems.
In your case, I assume the attached table inheritance is the choices:
class Anesthesia(Base) __tablename__ = 'anesthesias' id = Column(Integer, primary_key=True) anesthetic = Column(String)
The purpose of the discriminator field is:
... should act as a discriminator and store a value that indicates the type of object represented in the string. A column can be any type of data, although the row and integer are the most common.
Key
__mapper_args__ polymorphic_on determines which field is used as the discriminator. In child classes (below), the polymorphic_identity key determines the value that will be stored in the polymorphic discriminator column for class instances.
class InhalationAnesthesia(Anesthesia): __tablename__ = 'inhalation_anesthesias' __mapper_args__ = {'polymorphic_identity': 'inhalation'} id = Column(Integer, ForeignKey('anesthesias.id'), primary_key=True)
Finally, the Operation class can reference the Anesthesia parent table with a typical relationship:
class Operation(Base): __tablename__ = 'operations' id = Column(Integer, primary_key=True) anesthesia_id = Column(Integer, ForeignKey('anesthesias.id')) anesthesia = relationship('Anesthesia', backref='used_in_operations')
Hope this is what you are looking for.