Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!