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

时间:2016-03-08 16:40:40

标签: python mysql sqlalchemy

我试图根据这样的表达式过滤我的查询:

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

但是当我过滤它时,查询会将select语句添加两次,这在MYSQL中会导致一个OperationalError:1060“Duplicate column name'xxxx'。这是我的SQLAlchemy表达式:

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

只有在表达式中有多个AND时才会发生。我已经分割了字符串,以便更清楚地看到重复的选择。在下面的结果中是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']

任何帮助将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:0)

我明白了。 BinaryExpression实际上有两个表达式子句,因此第二个子句创建了另一个select。