Opened 8 years ago

Closed 8 years ago

#26959 closed Bug (duplicate)

AssertionError in query.py change_aliases — at Version 2

Reported by: Andrew Brown Owned by: nobody
Component: Database layer (models, ORM) Version: 1.9
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

I'm running into this AssertionError on a fairly complex query in my app, involving ORing two querysets together.

I've tried on both Django 1.9.8 and 1.10rc1

Here's the backtrace:

Traceback (most recent call last):
  File "/.../app/tests.py", line 32, in test_asserterror
    return qs1 | qs2
  File "/.../env/local/lib/python2.7/site-packages/django/db/models/query.py", line 318, in __or__
    combined.query.combine(other.query, sql.OR)
  File "/.../env/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 573, in combine
    w.relabel_aliases(change_map)
  File "/.../env/local/lib/python2.7/site-packages/django/db/models/sql/where.py", line 129, in relabel_aliases
    child.relabel_aliases(change_map)
  File "/.../env/local/lib/python2.7/site-packages/django/db/models/sql/where.py", line 131, in relabel_aliases
    self.children[pos] = child.relabeled_clone(change_map)
  File "/.../env/local/lib/python2.7/site-packages/django/db/models/lookups.py", line 103, in relabeled_clone
    new.rhs = new.rhs.relabeled_clone(relabels)
  File "/.../env/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 346, in relabeled_clone
    clone.change_aliases(change_map)
  File "/.../env/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 793, in change_aliases
    assert set(change_map.keys()).intersection(set(change_map.values())) == set()
AssertionError

And I've managed to simplify down a test case the best I could.

models.py:

class Group(models.Model):
    pass

class User(models.Model):
    groups = models.ManyToManyField(Group, related_name="members_of")
    friends = models.ManyToManyField('self')

test case:

g1 = models.Group.objects.create()
g2 = models.Group.objects.create()
g3 = models.Group.objects.create()

u1 = models.User.objects.create()

otherusers = models.User.objects.exclude(
    pk=u1.pk
).exclude(
    friends=u1
)

qs1 = otherusers.filter(
    groups=g1
).filter(
    groups=g2
)

qs2 = otherusers.filter(
    groups=g1
).filter(
    groups=g3
)

list(qs1 | qs2)

Let me know if I can provide any other information that would be helpful. Oddly enough in my app it only happens part of the time, seemingly at random, but this test case seems to hit the assertion every time for me.

Change History (2)

comment:1 by Andrew Brown, 8 years ago

I forgot to mention that I did see bug #24525, and this certainly seems related, but I'm still hitting this error in the latest versions of Django as stated above.

comment:2 by Tim Graham, 8 years ago

Description: modified (diff)
Resolution: duplicate
Status: newclosed

Looks like it's probably a duplicate of #26522.

Note: See TracTickets for help on using tickets.
Back to Top