以下のオブジェクトとリレーションが定義されています。これは実際には非常に単純なケースです。私は吸入と注射麻酔が2つの異なるクラスによって定義されるべきだと考える理由を示すために、これらのフィールドをすべて提供しています。sqlalchemyの複数の/クラスの関連付け
class InhalationAnesthesia(Base):
__tablename__ = "inhalation_anesthesias"
id = Column(Integer, primary_key=True)
anesthetic = Column(String)
concentration = Column(Float)
concentration_unit = Column(String)
duration = Column(Float)
duration_unit = Column(String)
class TwoStepInjectionAnesthesia(Base):
__tablename__ = "twostep_injection_anesthesias"
id = Column(Integer, primary_key=True)
anesthetic = Column(String)
solution_concentration = Column(Float)
solution_concentration_unit = Column(String)
primary_dose = Column(Float)
primary_rate = Column(Float)
primary_rate_unit = Column(String)
secondary_rate = Column(Float)
secondary_rate_unit = Column(String)
class Operation(Base):
__tablename__ = "operations"
id = Column(Integer, primary_key=True)
anesthesia_id = Column(Integer, ForeignKey('inhalation_anesthesias.id'))
anesthesia = relationship("InhalationAnesthesia", backref="used_in_operations")
私は、しかし、任意のOperation
オブジェクトがTwoStepInjectionAnesthesia
オブジェクトまたはInhalationAnesthesia
オブジェクトのいずれかを指すことができるようにOperation
クラスの麻酔属性を定義したいと思います。
どうすればいいですか?
この設定を使用すると、 'Operation.anesthesia'の下で' InhalationAnesthesia'と 'TwoStepInjectionAnesthesia'オブジェクトの両方を参照できるはずですか? 'Operation.anesthesia = [TwoStepInjectionAnesthesia(..)、InhalationAnesthesia(..)、Inhalationanesthesia(..)]を使用しますか? – TheChymera
@ TheChymeraはいとはい。提供された例では、「動作」インスタンスは1つの「麻酔」オブジェクトのみを参照する。それをリストにする必要がある場合は、コードを少し変更する必要があります。 –
さらにもう1つ、上記の設定を使用する利点は何ですか?生成される実際のSQLテーブルには、特定のディスクリミネータを持つ行の特定の列タイプのみが含まれますか? (これによりディスク容量を節約できますか?) – TheChymera