On Fri, 2008-10-17 at 21:03 -0700, Rares Vernica wrote: > Hello, > > I am using QuerySet "extra()" function. For the "tables" parameter I > need to specify the same table multiple times, so I included an alias > for each table. Unfortunately Django quotes the table names and > everything is messed up. > > The "tables" parameter for the "extra()" is: > > tables ['FName F0', 'FName_Person Fp0', 'FName F1', 'FName_Person Fp1'] > > Here is how the generated query looks like: > > SELECT COUNT(DISTINCT `Person`.`id`) > FROM > `Person` , > `FName F0` , > `FName_Person Fp0` , > `FName F1` , > `FName_Person Fp1` > WHERE ... > > If I remove the alias, Django does not realize that I give the same > table multiple times and does not generate any aliases. > > I really need the same table multiple times. > > Is there a way around this?
You can't do it in one call. I've spent a long time (better part of a year) wondering about what a reasonable API might be for this type of thing, but I think the conclusion is that it should be done in multiple steps, which means I need to document it and provide a public API to the right Query methods. The idea is that you call my_queryset.query.join(....) to add in the new table and it will return the alias you can use in the select statements when you call extra(). The call to join() will insert the tables into the query properly and you won't need to specify them in extra(tables=...). The docstring of Query.join() and a few experiments should get you started there. This sort of situation is edge-case enough (in fact using extra() is edge-case enough) that I'm reasonably comfortable saying you have to use multiple calls to get there. I've made a note to add something to the documentation about this. > > An easy fix might be to ask Django not to quote the "tables" > parameter. Is there a easy way to do this? No and that's intentional at the moment, Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---