SqlAlchemy使用one()然后join()和values()

时间:2017-09-09 16:33:08

标签: python orm sqlalchemy flask-sqlalchemy

我正在尝试过滤条件,并同时连接两个表来处理用户登录。用户名为Staff.staff_name,密码存储在Staff_Authentication.staff_password

        try:
            result = self.session.query(Staff).filter_by(staff_name=staff_name)\
                                .join(Staff_Authentication).one()\
                                .values(
                                        Staff.staff_id,
                                        Staff.staff_name,
                                        Staff_Authentication.staff_password,
                                        )
            if Staff_Authentication.check_password(staff_password, result.staff_password):
                # proceed to process valid login 
        except MultipleResultsFound as e:
            raise InvalidParameter("More than one pair of these name and password pair")
        except NoResultFound as e:
            raise InvalidParameter("No such user")

这会导致错误

AttributeError: 'Staff' object has no attribute 'values'

我试图在one()之后将filter_by()移到右边,结果类似

AttributeError: 'Staff' object has no attribute 'join'

使用join()后如何链接values()one()

1 个答案:

答案 0 :(得分:1)

one()first()all()等方法是最终的。你必须在其他一切之后使用它们。

        try:
            result = self.session.query(Staff).filter_by(staff_name=staff_name) \
                            .join(Staff_Authentication) \
                            .values(
                                    Staff.staff_id,
                                    Staff.staff_name,
                                    Staff_Authentication.staff_password,
                                    ) \
                            .one()
        if Staff_Authentication.check_password(staff_password, result.staff_password):
            # proceed to process valid login 
    except MultipleResultsFound as e:
        raise InvalidParameter("More than one pair of these name and password pair")
    except NoResultFound as e:
        raise InvalidParameter("No such user")