Python 将其添加到您的问题中,以便您可以格式化它们。感谢您的帮助。我已经在问题中添加了实际代码。不客气!我对你的代码有几点意见。1我记得==False有问题-您确定它不应该是==sa.False还是~Student.suspended?2运算符是用于什么的-您是在
Python 将其添加到您的问题中,以便您可以格式化它们。感谢您的帮助。我已经在问题中添加了实际代码。不客气!我对你的代码有几点意见。1我记得==False有问题-您确定它不应该是==sa.False还是~Student.suspended?2运算符是用于什么的-您是在,python,flask,sqlalchemy,flask-sqlalchemy,Python,Flask,Sqlalchemy,Flask Sqlalchemy,将其添加到您的问题中,以便您可以格式化它们。感谢您的帮助。我已经在问题中添加了实际代码。不客气!我对你的代码有几点意见。1我记得==False有问题-您确定它不应该是==sa.False还是~Student.suspended?2运算符是用于什么的-您是在执行位运算还是其他操作?3您不应加入StudentApplication,以防止出现多行。删除加入并将提交的检查移动到内部已批准的应用程序。4替换.filterStudent.query.filter~has_any_approved_app
将其添加到您的问题中,以便您可以格式化它们。感谢您的帮助。我已经在问题中添加了实际代码。不客气!我对你的代码有几点意见。1我记得==False有问题-您确定它不应该是==sa.False还是~Student.suspended?2运算符是用于什么的-您是在执行位运算还是其他操作?3您不应加入StudentApplication,以防止出现多行。删除加入并将提交的检查移动到内部已批准的应用程序。4替换.filterStudent.query.filter~has_any_approved_app with.filter~has_any_approved_app应该修复它我想我用你的建议更新了代码,并添加了sqlalchemy正在生成的sql。如果用户有匹配的任何学生而不是所有学生,则查询仍将返回用户。关于bitwise&运算符,这是有意的。看起来不错,我只是在为子查询创建别名时遇到了一个问题。来自中的sqlalchemy.exc.ProgrammingError:psycopg2.errors.SyntaxError子查询必须具有别名行2:FROM SELECT Child.name…^提示:例如,从选择。。。[AS]foo.EXISTS中的子查询不需要别名,因为它不在FROM中,并且您的代码没有提到任何其他子查询。你能展示你的代码吗?最好将它添加到你的问题中,这样你就可以格式化它们了,谢谢你的帮助。我已经在问题中添加了实际代码。不客气!我对你的代码有几点意见。1我记得==False有问题-您确定它不应该是==sa.False还是~Student.suspended?2运算符是用于什么的-您是在执行位运算还是其他操作?3您不应加入StudentApplication,以防止出现多行。删除加入并将提交的检查移动到内部已批准的应用程序。4替换.filterStudent.query.filter~has_any_approved_app with.filter~has_any_approved_app应该修复它我想我用你的建议更新了代码,并添加了sqlalchemy正在生成的sql。如果用户有匹配的任何学生而不是所有学生,则查询仍将返回用户。关于按位&运算符,这是有意的。
(Parent
.query
.join(Child)
.filter(Child.column == True)
).all()
def _active_student_query(statement=None):
import sqlalchemy as sa
statement = statement or _active_user_query()
statement = statement.join(Student).filter(Student.suspended == sa.false())
return statement
def users_without_submitted_applications(exclude_contacted=False):
import sqlalchemy as sa
from sqlalchemy.orm.util import AliasedClass
# AppAlias = AliasedClass(StudentApplication)
has_any_approved_app = sa.exists(
sa.select([])
.select_from(Student)
.where((StudentApplication.student_id == Student.id) &
(StudentApplication.flags.op('&')(AppFlags.SUBMITTED) > 0),
)
)
statement = _active_student_query()
statement = (statement
# .join(StudentApplication)
# .filter(User.students.any())
# has a student and no submitted applications
.filter(~has_any_approved_app)
# .filter(StudentApplication.flags.op('&')(AppFlags.SUBMITTED) == 0)
)
if exclude_contacted:
statement = (statement
.join(AlertPreferences)
.filter(AlertPreferences.marketing_flags.op('&')(MarketingFlags.NO_APP_PING) == 0)
)
from lib.logger import logger
logger.info(statement.sql)
return statement
SELECT users.is_active, users.flags, users.created_on, users.updated_on, users.id
FROM users JOIN student ON users.id = student.user_id
WHERE users.is_active = true AND student.suspended = false AND NOT (EXISTS (SELECT
FROM student_application
WHERE student_application.student_id = student.id AND (student_application.flags & %(flags_1)s) > %(param_1)s))
student_has_any_non_approved_app = sa.exists(
sa.select([])
.select_from(StudentApplication)
.where((StudentApplication.student_id == Student.id) &
(StudentApplication.flags.op('&')(AppFlags.SUBMITTED) > 0) &
((StudentApplication.flags.op('&')(AppFlags.APPROVED) == 0) |
(StudentApplication.flags.op('&')(AppFlags.PARTIALLY_APPROVED) == 0)))
)
user_has_any_non_approved_students = sa.exists(
sa.select([])
.select_from(Student)
.where((Student.user_id == User.id) &
(Student.suspended == sa.false()) &
student_has_any_non_approved_app)
)
statement = (
_active_user_query()
.filter(User.students.any())
# has a student and no submitted applications
.filter(~user_has_any_non_approved_students)
)