#28621: Crash in QuerySet.annotate() with OuterRef
-------------------------------------+-------------------------------------
     Reporter:  Дилян Палаузов       |                    Owner:  felixxm
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  master
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  QuerySet.extra       |             Triage Stage:  Ready for
                                     |  checkin
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Dmitry Mugtasimov):

 * cc: Dmitry Mugtasimov (added)
 * status:  closed => new
 * resolution:  fixed =>


Comment:

 {{{
 queryset.annotate(
                 has_role=Exists(
                     JobTitle.objects.annotate(
                         title_vector=SearchVector(
                             OuterRef('title'),
                             config=SIMPLE_PG_SEARCH_CONFIGURATION)
                     ).filter(
                         title_vector=SearchQuery(
                             F('name'),
 config=SIMPLE_PG_SEARCH_CONFIGURATION)
                     )
                 )
             )
 }}}

 results in


 {{{
 'ResolvedOuterRef' object has no attribute 'contains_aggregate'
 Request Method: GET
 Request URL:    http://127.0.0.1:8000/admin/jobs/job/
 Django Version: 2.2.2
 Exception Type: AttributeError
 Exception Value:
 'ResolvedOuterRef' object has no attribute 'contains_aggregate'
 Exception Location:     /usr/local/lib/python3.7/site-
 packages/django/db/models/expressions.py in <genexpr>, line 213
 Python Executable:      /usr/local/bin/python
 Python Version: 3.7.3
 Python Path:
 ['/code',
  '/code',
  '/usr/local/lib/python37.zip',
  '/usr/local/lib/python3.7',
  '/usr/local/lib/python3.7/lib-dynload',
  '/usr/local/lib/python3.7/site-packages']
 Server time:    Mon, 22 Jul 2019 09:22:47 +0000

 }}}



 {{{
 Environment:


 Request Method: GET
 Request URL: http://127.0.0.1:8000/admin/jobs/job/

 Django Version: 2.2.2
 Python Version: 3.7.3
 Installed Applications:
 ['core',
  'django.contrib.admin',
  'django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.messages',
  'django.contrib.staticfiles',
  'django.contrib.sites',
  'django.contrib.postgres',
  'allauth',
  'allauth.account',
  'drf_yasg',
  'rest_auth',
  'rest_auth.registration',
  'rest_framework',
  'rest_framework.authtoken',
  'colorful',
  'gm2m',
  'explorer',
  'allauth.socialaccount',
  'accounts',
  'questions',
  'colleges',
  'geo',
  'jobs',
  'advices',
  'referral']
 Installed Middleware:
 ['django.middleware.security.SecurityMiddleware',
  'django.contrib.sessions.middleware.SessionMiddleware',
  'django.middleware.common.CommonMiddleware',
  'django.middleware.csrf.CsrfViewMiddleware',
  'django.contrib.auth.middleware.AuthenticationMiddleware',
  'django.contrib.messages.middleware.MessageMiddleware',
  'django.middleware.clickjacking.XFrameOptionsMiddleware']



 Traceback:

 File "/usr/local/lib/python3.7/site-
 packages/django/core/handlers/exception.py" in inner
   34.             response = get_response(request)

 File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py"
 in _get_response
   115.                 response = self.process_exception_by_middleware(e,
 request)

 File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py"
 in _get_response
   113.                 response = wrapped_callback(request,
 *callback_args, **callback_kwargs)

 File "/usr/local/lib/python3.7/contextlib.py" in inner
   74.                 return func(*args, **kwds)

 File "/usr/local/lib/python3.7/site-
 packages/django/contrib/admin/options.py" in wrapper
   606.                 return self.admin_site.admin_view(view)(*args,
 **kwargs)

 File "/usr/local/lib/python3.7/site-packages/django/utils/decorators.py"
 in _wrapped_view
   142.                     response = view_func(request, *args, **kwargs)

 File "/usr/local/lib/python3.7/site-
 packages/django/views/decorators/cache.py" in _wrapped_view_func
   44.         response = view_func(request, *args, **kwargs)

 File "/usr/local/lib/python3.7/site-
 packages/django/contrib/admin/sites.py" in inner
   223.             return view(request, *args, **kwargs)

 File "/usr/local/lib/python3.7/site-packages/django/utils/decorators.py"
 in _wrapper
   45.         return bound_method(*args, **kwargs)

 File "/usr/local/lib/python3.7/site-packages/django/utils/decorators.py"
 in _wrapped_view
   142.                     response = view_func(request, *args, **kwargs)

 File "/usr/local/lib/python3.7/site-
 packages/django/contrib/admin/options.py" in changelist_view
   1672.             cl = self.get_changelist_instance(request)

 File "/usr/local/lib/python3.7/site-
 packages/django/contrib/admin/options.py" in get_changelist_instance
   744.             sortable_by,

 File "/usr/local/lib/python3.7/site-
 packages/django/contrib/admin/views/main.py" in __init__
   45.         self.root_queryset = model_admin.get_queryset(request)

 File "/code/jobs/admin.py" in get_queryset
   76.         return queryset.annotate_with_has_role()

 File "/code/jobs/models/job.py" in annotate_with_has_role
   68.                             config=SIMPLE_PG_SEARCH_CONFIGURATION)

 File "/usr/local/lib/python3.7/site-packages/django/db/models/manager.py"
 in manager_method
   82.                 return getattr(self.get_queryset(), name)(*args,
 **kwargs)

 File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py" in
 annotate
   1059.             if alias in annotations and
 annotation.contains_aggregate:

 File "/usr/local/lib/python3.7/site-packages/django/utils/functional.py"
 in __get__
   80.         res = instance.__dict__[self.name] = self.func(instance)

 File "/usr/local/lib/python3.7/site-
 packages/django/db/models/expressions.py" in contains_aggregate
   213.         return any(expr and expr.contains_aggregate for expr in
 self.get_source_expressions())

 File "/usr/local/lib/python3.7/site-
 packages/django/db/models/expressions.py" in <genexpr>
   213.         return any(expr and expr.contains_aggregate for expr in
 self.get_source_expressions())

 Exception Type: AttributeError at /admin/jobs/job/
 Exception Value: 'ResolvedOuterRef' object has no attribute
 'contains_aggregate'
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28621#comment:13>
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.b29619ef625fe1b873031630318bb389%40djangoproject.com.

Reply via email to