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

Viewed 47

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 ?

0 Answers
Related