我目前使用单表继承策略映射类层次结构(我无法使用joind)。该层次结构可能如下所示:
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)
那么问题是,我想有一property1两个Child1和Child2,但不是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)
谁能指出如何重用另一个子类已定义的列?
繁星点点滴滴
相关分类