【发布时间】:2013-06-14 15:00:19
【问题描述】:
我目前使用单表继承策略映射一个类层次结构(我不可能使用joined)。此层次结构可能如下所示:
class Parent(Base):
__tablename__ = 'mytable'
__mapper_args__ = {
'polymorphic_on' : type,
'polymorphic_identity' : 'parent'
}
id = Column(Integer, primary_key = True)
type = Column(String(32), nullable = False)
class Child1(Parent):
__mapper_args__ = { 'polymorphic_identity' : 'child1' }
property1 = Column(Integer)
class Child2(Parent):
__mapper_args__ = { 'polymorphic_identity' : 'child2' }
property1 = Column(Integer)
class Child3(Parent):
__mapper_args__ = { 'polymorphic_identity' : 'child3' }
other_property = Column(Integer)
问题是我想在Child1 和Child2 上都有一个property1,而不是在Child3 上。上面的当前代码导致错误:
sqlalchemy.exc.ArgumentError: Column 'property1' on class <class
'__main__.Child2'> conflicts with existing column 'mytable.property1'
我当然可以在继承层次结构中添加另一层,Child1 和 Child2 派生自并包含 property1 列,但 Child1 和 Child2 几乎没有相互关联,尽管我想要为两个类重用相同的数据库列。
我已经尝试将property1 = Child1.property1 添加到Child2,但没有奏效(Child2 的实例值未存储在数据库中)
谁能指出如何重用已经由另一个子类定义的列?
【问题讨论】:
标签: python orm sqlalchemy single-table-inheritance