我在GitHub上建立了Repositories的模型。每個回購可以有一組叉,其中回購將是其父母。SQLAlchemy中同一個表上的一對多關係
我的部分模型文件看起來像這樣:
class Repo(Base):
__tablename__ = "repos"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Text, unique=True)
parent_id = db.Column(Integer,db.ForeignKey('repos.id'))
parent = db.relationship("Repo")
我無法纏繞我的頭圍繞如何正確地使這種關係。
我還沒有嘗試用SQLAlchemy子類化模型,所以這是一個好主意。我嘗試了你的例子,但是出現這個錯誤:'SAWarning:無法爲本地映射表組裝任何主鍵'叉' - 沒有行將被保留在這個表中。 self._configure_pks()' –
它很可能是我的例子不正確,因爲我在飛行中用它來說明這個概念,但請查看http://docs.sqlalchemy.org/en/latest/orm/extensions /declarative/inheritance.html進行子類化...我編輯了一個明確的ID列設置爲叉爲叉 – Busturdust
你也可以研究'polymorphic_identity'在這個SO問題.... http://stackoverflow.com/questions/ 1337095/SQLAlchemy的繼承 – Busturdust