#25811: Error querying models in different databases in one queryset
-------------------------------------+-------------------------------------
     Reporter:  ebar0n               |                    Owner:  nobody
         Type:                       |                   Status:  new
  Cleanup/optimization               |
    Component:  Database layer       |                  Version:  master
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  database             |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by ebar0n):

 I've got the solution, you can see it in
 
[https://github.com/ebar0n/django/commit/0b5c344eb0c6437c83753f7fa1a6ea1faeb0c7cb]

 Tests conducted using '''debugsqlshell''':

 Two models that belong to the same database
 {{{
 Deportes.objects.filter(
     pk__in=Equipos.objects.all().values_list(
         'deporte_id', flat=True
     ).distinct('deporte_id')
 )
 ...
 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 (U0."deporte_id") U0."deporte_id" FROM
 "admin_juego_equipos" U0)
 ORDER BY "admin_juego_deportes"."nombre" ASC LIMIT 21 [291.41ms]
 }}}

 1 models, using as an argument a list
 {{{
 Deportes.objects.filter(
     pk__in=[1, 2]
 )
 ...
 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 LIMIT 21 [0.88ms]
 }}}

 Models belonging to two different databases
 {{{
 Deportes.objects.filter(
         pk__in=DimensionJuegos.objects.all().values_list(
                 'deporte_id', flat=True
         )
 )
 ...
 SELECT U0."deporte_id"
 FROM "admin_datamart_dimensionjuegos" U0 [0.68ms]
 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)
 ORDER BY "admin_juego_deportes"."nombre" ASC LIMIT 21 [1.74ms]
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25811#comment:11>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.4bc9f97d3cb0b9ccda503913cc6421ec%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to