【发布时间】:2014-05-28 01:50:38
【问题描述】:
我有一个分层类别模型,其中层次结构是使用物化路径(每级一个字符)维护的:
class Category(Base):
__tablename__ = 'categories'
id = Column(SmallInteger, primary_key=True)
path = Column(String, unique=True, nullable=False)
# problematic relationship
all_subcats = relationship('Category', lazy='dynamic', viewonly=True,
primaryjoin=foreign(path).like(remote(path).concat('%')))
在尝试定义“所有子类别”关系时,我遇到了一个问题:
sqlalchemy.exc.ArgumentError: Can't determine relationship direction for
relationship 'Category.all_subcats' - foreign key columns within the join
condition are present in both the parent and the child's mapped tables.
Ensure that only those columns referring to a parent column are marked as
foreign, either via the foreign() annotation or via the foreign_keys argument.
SQLAlchemy 很困惑,因为我加入了同一列。我设法找到的所有示例总是加入不同的列。
这种关系可能吗?我想通过这个join查询,所以自定义@property是不行的。
【问题讨论】:
-
您应该在sqlalchemy mailing list 上报告此类问题。我现在正在为此测试补丁。
-
哇,这真的很古怪。需要看看我是否可以提交。
标签: python sqlalchemy relationship declarative materialized-path-pattern