views:

245

answers:

2

Hello,

I am executing the following code (names changed to protect the innocent, so the model structure might seem weird):

memberships = 
    models.Membership.objects.filter(
        degree__gt=0.0,
        group=request.user.get_profile().group
    )

exclude_count =  
    memberships.filter(
        member__officerships__profile=request.user.get_profile()
    ).count()

if exclude_officers_with_profile:
    memberships = memberships.exclude(
        member__officerships__profile=request.user.get_profile()
    )

total_count = memberships.count()

which at this point results in:

OperationalError at /
(1054, "Unknown column 'U1.id' in 'on clause'")

The SQL generated is:

SELECT 
    COUNT(*) 
FROM 
    `membership` 
WHERE (
    `membership`.`group_id` = 2 AND
    `membership`.`level` > 0.0 AND 
    NOT (
        `membership`.`member_id` 
        IN (
            SELECT 
                U2.`member_id` 
            FROM 
                `membership` U0 INNER JOIN `officers` U2 
                ON (U1.`id` = U2.`member_id`) 
            WHERE U2.`profile_id` = 2 
        )
    )
)

It appears that the SQL Join's ON statement is referencing an alias that hasn't been defined. Any ideas why!? I dropped my MySQL database and re-synced the tables from my models to try and ensure that there weren't any inconsistencies there.

The structure of the models I'm using are:

class Membership(models.Model):
    member = models.ForeignKey(Member, related_name='memberships')
    group = models.ForeignKey(Group, related_name='memberships')
    level = models.FloatField(default=0.0)

class Member(models.Model):
    name = models.CharField(max_length=32)

class Officer(models.Model):
    member = models.ForeignKey(Member, related_name='officerships')
    profile = models.ForeignKey(UserProfile)

class UserProfile(models.Model)
    group = models.ForeignKey(Group)

class Group(models.Model)
    pass
+1  A: 

I think this may be a symptom of:

http://code.djangoproject.com/ticket/9188

which was fixed as of django revision 9589, I think. Now how to figure out which revision I'm working from...


Confirmed. When I implemented the change referenced in the ticket above:

http://code.djangoproject.com/changeset/9589

my error went away.

DGGenuine
To find your revision, go into ./manage.py shell and do:>>> import django>>> django.get_version()u'1.0-final-SVN-11012'
Daniel Roseman
When I use the `get_version` function I don't get a revision number, just '1.0-final'...darn.
DGGenuine
A: 

Awesome, I was getting this error too so a quick google search on the error found this post. The same patch fixed this for me.

Thanks for posting.

Tom Coote