python和表格关系_python – 单表继承上的SQLAlchemy一对多关系...
我之前正在努力解決這個問題.我能夠讓這種自我指涉關系發揮作用:
class Employee(Base):
__tablename__ = 'employee'
id = Column(Integer, primary_key=True)
name = Column(String(64), nullable=False)
Employee.manager_id = Column(Integer, ForeignKey(Employee.id))
Employee.manager = relationship(Employee, backref='subordinates',
remote_side=Employee.id)
請注意,manager和manager_id是“猴子修補的”,因為您無法在類定義中進行自引用.
所以在你的例子中,我猜這個:
class NodeTypeA(BaseNode):
__mapper_args__ = {'polymorphic_identity': 'NodeTypeA'}
typeB_children = relationship('NodeTypeB', backref='parent_node',
remote_side='NodeTypeB.parent_id')
編輯:基本上你的錯誤告訴你的是關系和它的backref是相同的.因此,無論SA使用何種規則來確定表級關系是什么,他們都不會對您提供的信息進行評分.
我了解到,在聲明性類中簡單地說mycolumn = relationship(OtherTable)將導致mycolumn成為一個列表,假設SA可以檢測到明確的關系.因此,如果您確實希望對象具有指向其父級而不是其子級的鏈接,則可以在子表中定義parent = relationship(OtherTable,backref =’children’,remote_side = OtherTable.id).這定義了父子關系的兩個方向.
總結
以上是生活随笔為你收集整理的python和表格关系_python – 单表继承上的SQLAlchemy一对多关系...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安装linux_linux安装mysql
- 下一篇: 树莓派python3.6 tensorf