As before, I have a Thing model with a many-to-many relationship to  
Source.

For a given Source, I want to find those Things that come only from  
that Source.

This SQL works fine in dbshell (on sqlite):

                 SELECT t
                 FROM (
                     SELECT thing.thing_id AS t, COUNT 
(thing.thing_id) AS c
                     FROM (
                         SELECT thing_id
                         FROM myapp_thing_sources
                         WHERE source_id = ...
                     ) AS thing, myapp_thing_sources
                     WHERE thing.thing_id = myapp_thing_sources.thing_id
                     GROUP BY thing.thing_id
                 )
                 WHERE c = 1;

(where ... is the id of the given Source)


But I can't seem to get it to work with extra(). Firstly I wasn't  
sure how to do a fully explicit select so I wrapped it in a WHERE id  
IN (...):

.extra(where=["""
             id IN (
                 SELECT t
                 FROM (
                     SELECT thing.thing_id AS t, COUNT 
(thing.thing_id) AS c
                     FROM (
                         SELECT thing_id
                         FROM myapp_thing_sources
                         WHERE source_id = %s
                     ) AS thing, myapp_thing_sources
                     WHERE thing.thing_id = myapp_thing_sources.thing_id
                     GROUP BY thing.thing_id
                 )
                 WHERE c = 1
             )
         """], params=[source.id]
         )

but that doesn't work either (even though the raw SQL does, even with  
the WHERE id IN (...). )

Any ideas?


James
--
James Tauber              http://jtauber.com/
journeyman of some   http://jtauber.com/blog/





--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to