#25811: Error in consultation with models of different database
----------------------------------------------+----------------------
     Reporter:  ebar0n                        |      Owner:  nobody
         Type:  Bug                           |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  1.8
     Severity:  Normal                        |   Keywords:  database
 Triage Stage:  Unreviewed                    |  Has patch:  0
Easy pickings:  0                             |      UI/UX:  0
----------------------------------------------+----------------------
 Model1: Deportes from database1
 Model2: Hecho1_VentasCadenasJuegos from database2

 Code:
 {{{
 Deportes.objects.filter(
     pk__in=Hecho1_VentasCadenasJuegos.objects.all().values_list(
         'juegos__deporte_id', flat=True
     ).distinct('juegos__deporte_id')
 )
 }}}

 Error:
 {{{
 Traceback (most recent call last):
   File "/usr/local/lib/python3.5/site-
 packages/django/db/backends/utils.py", line 64, in execute
     return self.cursor.execute(sql, params)
 psycopg2.ProgrammingError: relation
 "admin_datamart_hecho1_ventascadenasjuegos" does not exist
 LINE 1: ...ISTINCT ON (U1."deporte_id") U1."deporte_id" FROM "admin_dat...
                                                              ^


 The above exception was the direct cause of the following exception:

 Traceback (most recent call last):
   File "<console>", line 4, in <module>
   File "/usr/local/lib/python3.5/site-packages/django/db/models/query.py",
 line 138, in __repr__
     data = list(self[:REPR_OUTPUT_SIZE + 1])
   File "/usr/local/lib/python3.5/site-packages/django/db/models/query.py",
 line 162, in __iter__
     self._fetch_all()
   File "/usr/local/lib/python3.5/site-packages/django/db/models/query.py",
 line 965, in _fetch_all
     self._result_cache = list(self.iterator())
   File "/usr/local/lib/python3.5/site-packages/django/db/models/query.py",
 line 238, in iterator
     results = compiler.execute_sql()
   File "/usr/local/lib/python3.5/site-
 packages/django/db/models/sql/compiler.py", line 840, in execute_sql
     cursor.execute(sql, params)
   File "/usr/local/lib/python3.5/site-
 packages/django/db/backends/utils.py", line 79, in execute
     return super(CursorDebugWrapper, self).execute(sql, params)
   File "/usr/local/lib/python3.5/site-
 packages/django/db/backends/utils.py", line 64, in execute
     return self.cursor.execute(sql, params)
   File "/usr/local/lib/python3.5/site-packages/django/db/utils.py", line
 97, in __exit__
     six.reraise(dj_exc_type, dj_exc_value, traceback)
   File "/usr/local/lib/python3.5/site-packages/django/utils/six.py", line
 658, in reraise
     raise value.with_traceback(tb)
   File "/usr/local/lib/python3.5/site-
 packages/django/db/backends/utils.py", line 64, in execute
     return self.cursor.execute(sql, params)
 django.db.utils.ProgrammingError: relation
 "admin_datamart_hecho1_ventascadenasjuegos" does not exist
 LINE 1: ...ISTINCT ON (U1."deporte_id") U1."deporte_id" FROM "admin_dat...

 }}}

 Viewing the SQL generated
 {{{
 SELECT "admin_juego_deportes"."id", "admin_juego_deportes"."nombre",
 "admin_juego_deportes"."logo", "admin_juego_deportes"."orden",
 "admin_juego_deportes"."orden_equipos",
 "admin_juego_deportes"."count_apuesta", "admin_juego_deportes"."fondoweb",
 "admin_juego_deportes"."cantidad",
 "admin_juego_deportes"."cantidad_tiempos",
 "admin_juego_deportes"."runline_positivo",
 "admin_juego_deportes"."ganador_empate_not_null",
 "admin_juego_deportes"."resultado", "admin_juego_deportes"."created_at",
 "admin_juego_deportes"."updated_at" FROM "admin_juego_deportes" WHERE
 "admin_juego_deportes"."id" IN (SELECT DISTINCT ON (U1."deporte_id")
 U1."deporte_id" FROM "admin_datamart_hecho1_ventascadenasjuegos" U0 INNER
 JOIN "admin_datamart_dimensionjuegos" U1 ON ( U0."juegos_id" = U1."id" ))
 ORDER BY "admin_juego_deportes"."nombre" ASC
 }}}

 Solution:
 {{{
 Deportes.objects.filter(
     pk__in=list(Hecho1_VentasCadenasJuegos.objects.all().values_list(
         'juegos__deporte_id', flat=True
     ).distinct('juegos__deporte_id'))
 )
 }}}

 Viewing the SQL generated
 {{{
 SELECT "admin_juego_deportes"."id", "admin_juego_deportes"."nombre",
 "admin_juego_deportes"."logo", "admin_juego_deportes"."orden",
 "admin_juego_deportes"."orden_equipos",
 "admin_juego_deportes"."count_apuesta", "admin_juego_deportes"."fondoweb",
 "admin_juego_deportes"."cantidad",
 "admin_juego_deportes"."cantidad_tiempos",
 "admin_juego_deportes"."runline_positivo",
 "admin_juego_deportes"."ganador_empate_not_null",
 "admin_juego_deportes"."resultado", "admin_juego_deportes"."created_at",
 "admin_juego_deportes"."updated_at" FROM "admin_juego_deportes" WHERE
 "admin_juego_deportes"."id" IN (1, 2) ORDER BY
 "admin_juego_deportes"."nombre" ASC
 }}}

 Nevertheless, the problem is given in the subquery that incrupta in the
 first query.

 In the ORM should be verified that both models belong to different data
 base and generate the correct SQL, evaluating the second and not generate
 a subquery.

--
Ticket URL: <https://code.djangoproject.com/ticket/25811>
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/049.7287e4ba2d631d904ba36a7aa27b63f9%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to