简体   繁体   English

SQLAlchemy多个AND表达式在同一条目上创建多个选择

[英]SQLAlchemy multiple AND expression creates multiple selects on same entry

I am trying to filter my query based on an expression like so: 我试图基于这样的表达式来过滤查询:

self.query = self.query.filter(expression)

But when I filter it, the query adds the select statement twice, which in MYSQL causes an OperationalError: 1060 "Duplicate column name 'xxxx'. Here is my SQLAlchemy expression: 但是,当我对其进行过滤时,查询将两次添加选择语句,这在MYSQL中会导致OperationalError:1060“重复的列名'xxxx'。这是我的SQLAlchemy表达式:

'"var__490__07McUdXtiy".response IS NOT NULL AND "var__490__07McUdXtiy".response != :param_1 AND "var__490__07McUdXtiy".response = :param_2'

It only happens when I have more than one AND in the expression. 仅当表达式中包含多个AND时,才会发生这种情况。 I have split the string in order to more clearly see the duplicate select. 我将字符串分割开以便更清楚地看到重复的选择。 It is var__490__07McUdXtiy in the result below: 以下结果是var__490__07McUdXtiy:

['SELECT auth_user.id AS auth_user_id',
 ' auth_user.password AS auth_user_password',
 ' auth_user.last_login AS auth_user_last_login',
 ' auth_user.is_superuser AS auth_user_is_superuser',
 ' auth_user.username AS auth_user_username',
 ' auth_user.first_name AS auth_user_first_name',
 ' auth_user.last_name AS auth_user_last_name',
 ' auth_user.email AS auth_user_email',
 ' auth_user.is_staff AS auth_user_is_staff',
 ' auth_user.is_active AS auth_user_is_active',
 ' auth_user.date_joined AS auth_user_date_joined',
 ' `var__490__07McUdXtiy`.response AS `07McUdXtiy_short`',
 ' `var__490__07McUdXtiy`.response AS `07McUdXtiy_short` \nFROM auth_user INNER JOIN reviewroom_reviewroomgroup_members ON reviewroom_reviewroomgroup_members.user_id = auth_user.id AND reviewroom_reviewroomgroup_members.reviewroomgroup_id IN (%s',
 ' %s) LEFT OUTER JOIN surveys_responsegroup AS survey__490 ON survey__490.user_id = auth_user.id AND survey__490.deleted = %s AND survey__490.survey_id = %s LEFT OUTER JOIN surveys_response AS `var__490__07McUdXtiy` ON `var__490__07McUdXtiy`.group_id = survey__490.id AND `var__490__07McUdXtiy`.question = %s \nWHERE `var__490__07McUdXtiy`.response IS NOT NULL AND `var__490__07McUdXtiy`.response != %s AND `var__490__07McUdXtiy`.response = %s']

Any help would be appreciated, thanks! 任何帮助,将不胜感激,谢谢!

I figured it out. 我想到了。 The BinaryExpression actually had two expression clauses and so the second clause created another select. BinaryExpression实际上有两个表达式子句,因此第二个子句创建了另一个选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM