SQLAlchemy 中的多对多递归 table 定义导致 AmbiguousForeignKeysError
Many-to-many recursive table definition in SQLAlchemy results in AmbiguousForeignKeysError
我正在尝试创建 table 的 配置文件 ,其中配置文件、测试和文件夹可以是配置文件的子项,但我得到一个 AmbiguousForeignKeysError 当我调用 create_all(db)
时。我对单个 table 的想法,然后通过映射器或关联 table 引用自身似乎应该可行,但我无法让 sqlalchemy 合作。我使用 sqlalchemy 网站上的多对多关系示例来创建我的代码。这是我的片段:
from sqlalchemy import Column, Integer, String, Enum, Table, ForeignKey
from sqlalchemy.orm import relationship,validates
from core.coyote.models import Base #declarative base
association_table = Table('association', Base.metadata,
Column('left_id', Integer, ForeignKey('profile_member.id')),
Column('right_id', Integer, ForeignKey('profile_member.id'))
)
class ProfileMember(Base):
__tablename__ = "profile_member"
id = Column(Integer, primary_key=True)
name = Column(String(250),nullable=True)
type = Column(Enum("Test","Profile","Folder"))
children = relationship("ProfileMember",secondary=association_table)
我还计划放入一些验证码,以便我可以强制执行一些出身规则。这是我得到的完整错误:
sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join condition between parent/child tables on relationship ProfileMember.children - there are multiple foreign key paths linking the tables via secondary table 'association'. Specify the 'foreign_keys' argument, providing a list of those columns which should be counted as containing a foreign key reference from the secondary table to each of the parent and child tables
任何关于我可以尝试的事情的线索都将不胜感激。
您必须指定如何加入辅助 table:
children = relationship("ProfileMember", secondary=association_table,
primaryjoin=id == association_table.c.left_id,
secondaryjoin=association_table.c.right_id == id)
我正在尝试创建 table 的 配置文件 ,其中配置文件、测试和文件夹可以是配置文件的子项,但我得到一个 AmbiguousForeignKeysError 当我调用 create_all(db)
时。我对单个 table 的想法,然后通过映射器或关联 table 引用自身似乎应该可行,但我无法让 sqlalchemy 合作。我使用 sqlalchemy 网站上的多对多关系示例来创建我的代码。这是我的片段:
from sqlalchemy import Column, Integer, String, Enum, Table, ForeignKey
from sqlalchemy.orm import relationship,validates
from core.coyote.models import Base #declarative base
association_table = Table('association', Base.metadata,
Column('left_id', Integer, ForeignKey('profile_member.id')),
Column('right_id', Integer, ForeignKey('profile_member.id'))
)
class ProfileMember(Base):
__tablename__ = "profile_member"
id = Column(Integer, primary_key=True)
name = Column(String(250),nullable=True)
type = Column(Enum("Test","Profile","Folder"))
children = relationship("ProfileMember",secondary=association_table)
我还计划放入一些验证码,以便我可以强制执行一些出身规则。这是我得到的完整错误:
sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join condition between parent/child tables on relationship ProfileMember.children - there are multiple foreign key paths linking the tables via secondary table 'association'. Specify the 'foreign_keys' argument, providing a list of those columns which should be counted as containing a foreign key reference from the secondary table to each of the parent and child tables
任何关于我可以尝试的事情的线索都将不胜感激。
您必须指定如何加入辅助 table:
children = relationship("ProfileMember", secondary=association_table,
primaryjoin=id == association_table.c.left_id,
secondaryjoin=association_table.c.right_id == id)