I've got a complex query that uses a combination of Q objects and
qargs to produce the following query:

Note the T4 alias...this is central to the question.

SELECT
`t_answer`.`id`,
`t_answer`.`question_id`,
`t_answer`.`survey_id`,
`t_answer`.`content_type_id`,
`t_intanswer`.`answer_ptr_id`,
`t_intanswer`.`value`
FROM
`t_intanswer`
INNER JOIN `t_answer` ON (`t_intanswer`.`answer_ptr_id` =
`t_answer`.`id`)
INNER JOIN `t_survey` ON (`t_answer`.`survey_id` = `t_survey`.`id`)
INNER JOIN `t_answer` T4 ON (`t_survey`.`id` = T4.`survey_id`)
INNER JOIN `t_booleananswer` ON (T4.`id` =
`t_booleananswer`.`answer_ptr_id`)
INNER JOIN `t_choiceanswer` ON (T4.`id` =
`t_choiceanswer`.`answer_ptr_id`)
INNER JOIN `t_facility` ON (`t_survey`.`facility_id` =
`t_facility`.`id`)
WHERE
(
    (
        (`t_booleananswer`.`value` = True  AND T4.`question_id` =
1742) AND
        (`t_choiceanswer`.`choice_id` = 1947  AND T4.`question_id` =
1743 )
    ) AND
`t_survey`.`public` = False  AND
`t_survey`.`facility_id` = 82  AND
`t_survey`.`doctor_id` = 124  AND
`t_answer`.`question_id` = 1753  AND
`t_facility`.`program_id` = 71  AND
`t_survey`.`completed` IS NOT NULL AND
`t_survey`.`flagged` = False  AND
`t_survey`.`completed` < '2012-05-20 00:00:00'  AND
`t_survey`.`completed` > '2012-05-18 00:00:00' )


I'm looking for a result that contains either a boolean or choice
answer, but I've used Django's model inheritance, and both tables are
trying to join through t_answer.

Only one t_answer row can be returned for this query, so I get back
zero results.  But I need one result.  Here is the modified query in
raw SQL that solves my problem.  I create a second FROM clause using
the alias T5, then I join choice on that.   Works great.


SELECT
`t_answer`.`id`,
`t_answer`.`question_id`,
`t_answer`.`survey_id`,
`t_answer`.`content_type_id`,
`t_intanswer`.`answer_ptr_id`,
`t_intanswer`.`value`
FROM
`t_intanswer`
INNER JOIN `t_answer` ON (`t_intanswer`.`answer_ptr_id` =
`t_answer`.`id`)
INNER JOIN `t_survey` ON (`t_answer`.`survey_id` = `t_survey`.`id`)
INNER JOIN `t_answer` T4 ON (`t_survey`.`id` = T4.`survey_id`)
INNER JOIN `t_answer` T5 ON (`t_survey`.`id` = T5.`survey_id`)
INNER JOIN `t_booleananswer` ON (T4.`id` =
`t_booleananswer`.`answer_ptr_id`)
INNER JOIN `t_choiceanswer` ON (T5.`id` =
`t_choiceanswer`.`answer_ptr_id`)
INNER JOIN `t_facility` ON (`t_survey`.`facility_id` =
`t_facility`.`id`)
WHERE
(
    (
        (`t_booleananswer`.`value` = True  AND T4.`question_id` =
1742) AND
        (`t_choiceanswer`.`choice_id` = 1947  AND T5.`question_id` =
1743 )
    ) AND
`t_survey`.`public` = False  AND
`t_survey`.`facility_id` = 82  AND

Cool...so I have my solution---except that I don't know how to
construct it using the Django queryset.  I looked at "extra" (https://
docs.djangoproject.com/en/1.3/ref/models/querysets/#extra) but I'm not
clear on how to use it accomplish my second INNER JOIN T5.

Any thoughts experts?  Thanks

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to