sqlalchemy.exc.InvalidRequestError: Don’t know how to join to <sqlalchemy.sql.selectable.Select object at 0x00000118E0DA2908>. Please use the .select_from() method to establish an explicit left side, as well as providing an explicit ON clause if not present already to help resolve the ambiguity.
错误提示信息意思大概是:
我的代码是:
as= db.session.query(B.id).join(另一个查询语句, B.ancestors.like(
func.find_in_set(item[1].ancestors, B.ancestors))).group_by(B.id).all()
sql = as.statement.compile(compile_kwargs={"literal_binds": True})
data = as.paginate(page=1, per_page=10, error_out=False)
print(sql)
我想要另一个查询出来的查询语句得出来的表能够连接另一张表,想使用内连接,query是select语句中from,是来自哪张表的意思,总的来说我没弄懂基本概念就开始行动妄想搭建高层,join(内连接)和outerjoin(外连接)()中先写连接哪张表然后写on的条件,也就是连接的条件语句。
之后又出现错误:
sqlalchemy.exc.ArgumentError: Column expression or FROM clause expected, got <sqlalchemy.sql.selectable.Select object at 0x000002019FC8C448> object resolved from <flask_sqlalchemy.BaseQuery object at 0x000002019FBDE248> object. To create a FROM clause from a <class ‘sqlalchemy.sql.selectable.Select’> object, use the .subquery() method. (Background on this error at: http://sqlalche.me/e/14/89ve)
没理解对query的使用,ok这样写才是正确的:
softwares = db.session.query(SoftwareModel, SoftwareAuth).outerjoin(
SoftwareAuth,
SoftwareAuth.software_id == SoftwareModel.id).outerjoin(
DepartmentModel,
SoftwareAuth.dept_id == DepartmentModel.id).filter(
or_(func.find_in_set(dept[1].id, DepartmentModel.ancestors), DepartmentModel.id == dept[1].id,
SoftwareModel.auth_type == 1), *filters
).paginate(page=res.page, per_page=res.limit, error_out=False)