首页 > 解决方案 > 使用声明性 SQLAlchemy 查询时失败:sa_exc.InvalidRequestError

问题描述

我正在使用 Python 3.9,SQLAlchemy 1.3.23。我正在尝试运行下面的查询(声明性),但它不起作用:

    def get_user_rights_standard(self) -> List[Tuple[str, str, str]]:
        
        return self.session.query(Employee, SvnRepo, SvnRight).\
            with_entities(Employee.username.label("username"), SvnRepo.name.label("svn_repo"), SvnRight.name.label("svn_access_right")).\
                join(SvnGroupMember, and_(SvnGroupMember.employee_id == Employee.id, SvnRepo.id == SvnGroupMember.repo_id)).\
                    join(SvnGroup, SvnGroup.id == SvnGroupMember.group_id).\
                        join(SvnRepoPathRight, and_(SvnGroupMember.group_id == SvnRepoPathRight.group_id, SvnGroupMember.repo_id == SvnRepoPathRight.repo_id, SvnRepoPathRight.right_id == SvnRight.id)).\
                            join(AuthzType, AuthzType.id == SvnRepo.authz_type_id).\
                                join(Path, Path.id == SvnRepoPathRight.path_id).\
                                    filter(and_(AuthzType.name == "Standard", Employee.username is not None)).\
                                        group_by(Employee.username).\
                                            all()

错误消息说:

raise sa_exc.InvalidRequestError(
sqlalchemy.exc.InvalidRequestError: Can't determine which FROM clause to join from, there are multiple FROMS which can join to this entity. Please use the .select_from() method to establish an explicit left side, as well as providing an explcit ON clause if not present already to help resolve the ambiguity.

我也尝试过select_from(),但它从来没有用过,并且总是因同样的错误而失败。

如何解决?

我提前谢谢你。

问候,

杰克

标签: sqlalchemypython-3.9

解决方案


推荐阅读