我有一个分层类别模型,其中使用物化路径(每级一个字符)维护层次结构:
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 很困惑,因为我正在加入同一列。我设法找到的所有示例总是加入不同的列。
这种关系有可能吗?我想通过这个连接查询,所以自定义@property 是不可接受的。