sqlalchemy中的多个/拆分类关联 [英] multiple/split class associations in sqlalchemy
问题描述
我定义了以下对象和关系.这实际上是一个非常简单的案例,我提供所有这些字段只是为了说明为什么我相信吸入麻醉和注射麻醉应该由两个不同的类别来定义.
I have the following objects and relations defined. This is actually quite a simple case, and I am providing all those fields just to show why I believe inhalation and injection anesthesia should be defined by two different classes.
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
类的麻醉属性,以使任何Operation
对象都可以指向TwoStepInjectionAnesthesia
对象或InhalationAnesthesia
对象.
I would, however, like to define the anesthetic attribute of the Operation
class in such a way that any Operation
object can point to either a TwoStepInjectionAnesthesia
object or an InhalationAnesthesia
object.
我该怎么做?
推荐答案
我建议您使用继承.在SqlAlchemy docs 此处和此处
I suggest you to use inheritance. It's very, very well explained in SqlAlchemy docs here and here
我的建议是创建一个Anesthesia
类,并使InhalationAnesthesia
和TwoStepInjectionAnesthesia
都继承自该类.您可以决定使用哪种类型的表继承:
My recommendation is to create an Anesthesia
class and make both InhalationAnesthesia
and TwoStepInjectionAnesthesia
inherit from it. It's your call to decide which type of table inheritance use:
- 单表继承
- 具体表继承
- 联接表继承
最常见的继承形式是单表和联接表, 而具体的继承则带来了更多的配置挑战.
The most common forms of inheritance are single and joined table, while concrete inheritance presents more configurational challenges.
对于您的情况,我假设联接表继承是选举:
For your case I'm asuming joined table inheritance is the election:
class Anesthesia(Base)
__tablename__ = 'anesthesias'
id = Column(Integer, primary_key=True)
anesthetic = Column(String)
# ...
# every common field goes here
# ...
discriminator = Column('type', String(50))
__mapper_args__ = {'polymorphic_on': discriminator}
discriminator
字段的目的:
...将充当区分符并存储 一个指示行中表示的对象类型的值. 该列可以是任何数据类型,尽管string和integer是 最常见.
... is to act as the discriminator, and stores a value which indicates the type of object represented within the row. The column may be of any datatype, though string and integer are the most common.
__mapper_args__
的 polymorphic_on 键定义哪个字段用作区分符.
在子类(如下)中, polymorphic_identity 键定义了将存储在该类实例的多态鉴别符列中的值.
__mapper_args__
's polymorphic_on key define which field use as discriminator.
In children classes (below), polymorphic_identity key define the value that will be stored in the polymorphic discriminator column for instances of the class.
class InhalationAnesthesia(Anesthesia):
__tablename__ = 'inhalation_anesthesias'
__mapper_args__ = {'polymorphic_identity': 'inhalation'}
id = Column(Integer, ForeignKey('anesthesias.id'), primary_key=True)
# ...
# specific fields definition
# ...
class TwoStepInjectionAnesthesia(Anesthesia):
__tablename__ = 'twostep_injection_anesthesias'
__mapper_args__ = {'polymorphic_identity': 'twostep_injection'}
id = Column(Integer, ForeignKey('anesthesias.id'), primary_key=True)
# ...
# specific fields definition
# ...
最后,Operation
类可以通过典型关系引用父表Anesthesia
:
Finally the Operation
class may reference the parent table Anesthesia
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')
希望这就是您想要的.
这篇关于sqlalchemy中的多个/拆分类关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!