#33319: Query.change_aliases raises an AssertionError
-------------------------------------+-------------------------------------
     Reporter:  Ömer Faruk Abacı     |                    Owner:  Vishal
                                     |  Pandey
         Type:  Bug                  |                   Status:  assigned
    Component:  Database layer       |                  Version:  3.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Vishal Pandey):

 Replying to [comment:4 Simon Charette]:

 Thank you for your helping hand.
 I tried two approaches to solve this issue by tweaking `table_alias`
 method of Query class, and it works with the above-attested sample code,
 but unfortunately, both of them fail with few testcases.
 By both the approaches, I am trying to resolve the conflicts between the
 change_map's keys and its values

 1st approach:
 {{{
         if alias_list:
             from random import choice
             alias = '%s%d' % (choice(ascii_uppercase), len(self.alias_map)
 + 1)
             alias_list.append(alias)
 }}}
 Here, I am randomly choosing an uppercase letter as the first character of
 alias, this returns different aliases, but a test_case is failing with
 this approach, with the following traceback.

 {{{
 FAIL: test_multiple_search_fields (admin_changelist.tests.ChangeListTests)
 [<object object at 0x7fc0aa886cf0>] (search_string='Tiny Desk Concert')
 All rows containing each of the searched words are returned, where each
 ----------------------------------------------------------------------
 Traceback (most recent call last):
   File "/usr/lib/python3.8/unittest/case.py", line 60, in testPartExecutor
     yield
   File "/usr/lib/python3.8/unittest/case.py", line 582, in subTest
     yield
   File
 "/home/vishal/Desktop/open_source/django/tests/admin_changelist/tests.py",
 line 550, in test_multiple_search_fields
     group_changelist = group_model_admin.get_changelist_instance(request)
   File
 "/home/vishal/Desktop/open_source/django/django/contrib/admin/options.py",
 line 742, in get_changelist_instance
     return ChangeList(
   File
 "/home/vishal/Desktop/open_source/django/django/contrib/admin/views/main.py",
 line 100, in __init__
     self.queryset = self.get_queryset(request)
   File
 "/home/vishal/Desktop/open_source/django/django/contrib/admin/views/main.py",
 line 498, in get_queryset
     qs = self.root_queryset.filter(Exists(qs))
   File
 "/home/vishal/Desktop/open_source/django/django/db/models/query.py", line
 977, in filter
     return self._filter_or_exclude(False, args, kwargs)
   File
 "/home/vishal/Desktop/open_source/django/django/db/models/query.py", line
 995, in _filter_or_exclude
     clone._filter_or_exclude_inplace(negate, args, kwargs)
   File
 "/home/vishal/Desktop/open_source/django/django/db/models/query.py", line
 1002, in _filter_or_exclude_inplace
     self._query.add_q(Q(*args, **kwargs))
   File
 "/home/vishal/Desktop/open_source/django/django/db/models/sql/query.py",
 line 1374, in add_q
     clause, _ = self._add_q(q_object, self.used_aliases)
   File
 "/home/vishal/Desktop/open_source/django/django/db/models/sql/query.py",
 line 1395, in _add_q
     child_clause, needed_inner = self.build_filter(
   File
 "/home/vishal/Desktop/open_source/django/django/db/models/sql/query.py",
 line 1263, in build_filter
     condition = filter_expr.resolve_expression(self,
 allow_joins=allow_joins)
   File
 "/home/vishal/Desktop/open_source/django/django/db/models/expressions.py",
 line 248, in resolve_expression
     c.set_source_expressions([
   File
 "/home/vishal/Desktop/open_source/django/django/db/models/expressions.py",
 line 249, in <listcomp>
     expr.resolve_expression(query, allow_joins, reuse, summarize)
   File
 "/home/vishal/Desktop/open_source/django/django/db/models/sql/query.py",
 line 1035, in resolve_expression
     clone.bump_prefix(query)
   File
 "/home/vishal/Desktop/open_source/django/django/db/models/sql/query.py",
 line 925, in bump_prefix
     self.change_aliases({
   File
 "/home/vishal/Desktop/open_source/django/django/db/models/sql/query.py",
 line 848, in change_aliases
     assert set(change_map).isdisjoint(change_map.values())
 AssertionError
 }}}

 In 2nd approach, I have rotated the `alias_prefix` in a circular manner
 from T(class variable `alias_prefix` has value T) to Z, then from A to Z,
 and so on, and it also works on the above-attested code but fails five
 other test cases.
 {{{
         if alias_list:
             alias = '%s%d' % (self.alias_prefix, len(self.alias_map) + 1)
             self.alias_prefix = chr(ord(self.alias_prefix)+1) if
 chr(ord(self.alias_prefix)+1) <= 'Z' else 'A'
             alias_list.append(alias)
 }}}
 I  feel something needs to be tweaked here only to solve the problem, but
 because of my limited knowledge of the underlying codebase, I am not being
 able to solve it.
 I seek help from my fellow senior developers to solve this issue.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33319#comment:7>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/072.f7b4a7cb9c67ec7db71ca9626aa28409%40djangoproject.com.

Reply via email to