Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

262
Vistas
Why Django doesn't wrap [] or SELECT to ARRAY in case of using Array field?

For example I have such query as:

Chat.objects.filter(users__contains=[user.pk]).filter(users__contained_by=mentors.values_list('pk', flat=True)) 

This turns to such query:

SELECT "chat_chat"."created_at", "chat_chat"."updated_at", "chat_chat"."id", "chat_chat"."users"
FROM "chat_chat"
 WHERE 
   ("chat_chat"."users" @> [1]::integer[] AND
    "chat_chat"."users" <@ (SELECT V0."id" FROM "users_user" V0
         INNER JOIN "users_userrequest_mentors" V1 ON (V0."id" = V1."user_id")
         WHERE V1."userrequest_id" IN
          (SELECT U0."id" FROM "users_userrequest" U0 WHERE U0."user_id" = 1))::integer[])

If I run this query , I'll face the problem of casting.

cannot cast integer to integer[]

But the documentation of Postgres says that arrays should be used as ARRAY[1,2,...,4]
So I've wrapped [1] and (SELECT ...) to ARRAY like

SELECT "chat_chat"."created_at", "chat_chat"."updated_at", "chat_chat"."id", "chat_chat"."users"
FROM "chat_chat"
 WHERE 
   ("chat_chat"."users" @> ARRAY[1]::integer[] AND
    "chat_chat"."users" <@ ARRAY(SELECT V0."id" FROM "users_user" V0
         INNER JOIN "users_userrequest_mentors" V1 ON (V0."id" = V1."user_id")
         WHERE V1."userrequest_id" IN
          (SELECT U0."id" FROM "users_userrequest" U0 WHERE U0."user_id" = 1))::integer[])

And everything works fine.
I'm just curious why Django (or psycopg2) skip this wrapping. Is there any special meaning for such behavior ?

over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda