【发布时间】:2013-03-10 00:32:12
【问题描述】:
所以我在 SQLAlchemy (0.8) 中有这种一对多的关系:
class Parent(Base):
__tablename__ = "parents"
cid = Column(Integer(11), primary_key = True, autoincrement = False)
uid = Column(Integer(11), ForeignKey('otherTable.uid',
ondelete = 'CASCADE'), primary_key = True)
...
# Relationship with child
childs_rel = relationship("Child", backref = 'parents',
cascade = "all, delete-orphan")
和
class Child(Base):
__tablename__ = "childs"
mid = Column(Integer(11), primary_key = True, autoincrement = False)
cid = Column(Integer(11), ForeignKey('parents.cid',
ondelete = 'CASCADE'), primary_key = True)
uid = Column(Integer(11), ForeignKey('parents.uid',
ondelete = 'CASCADE'), primary_key = True)
...
我可以创建这个数据库,但是当我尝试操作它时,我得到了这个错误:
sqlalchemy.exc.AmbiguousForeignKeysError: 无法确定加入 关系上父/子表之间的条件 Parent.childs_rel - 有多个外键路径链接表。指定“foreign_keys”参数,提供那些列的列表 应计为包含对父级的外键引用 表。
我试图在 childs_rel 中指定“foreign_keys”,但它说 Parent 类中没有外键,这是真的……必须在 child 类中指定,但根据 SQLAlchemy 的 ORM 文档,定义了关系在“一对多”关系中的“一”中...
http://docs.sqlalchemy.org/en/rel_0_8/orm/relationships.html#one-to-many
您认为这里发生了什么? 非常感谢!
【问题讨论】:
标签: python mysql sqlalchemy one-to-many