Multidimensional Array With a NULL Element Inside an Internal Array

So I have this table:

CREATE TABLE test (
    id_doc integer not null,
    multi  timestamp without time zone[]
);

The multi column should represent data similar to this JSON array:

[
    ['2017-02-03T17:20:00', '2017-02-03T17:20:01'],
    ['2017-02-01T16:21:55', '2017-02-02T13:23:03'],
]

It works fine, unless one of the internal arrays is empty. When I try to insert:

INSERT INTO test (id_doc, multi)
    VALUES (1, ARRAY[ARRAY[NULL], ARRAY['2017-02-03T17:01:30'::timestamp]]);

Postgres (9.3) gives me the following error:

ERROR:  ARRAY could not convert type timestamp without time zone[] to text[]
LINE 2: VALUES (1, ARRAY[ARRAY[NULL], ARRAY['2017-03-02T16:53:00'::t...
                                      ^

Although the error points to the second element (ARRAY ['2017-02-03T17: 01: 30' :: timestamp]), the problem seems to be related to the first (ARRAY [NULL]), because I can force it work if I change the INSERT statement to:

INSERT INTO test (id_doc, multi)
    VALUES (1, ARRAY[ARRAY[NULL::timestamp], ARRAY['2017-02-03T17:01:30'::timestamp]]);

Unfortunately, I am using SQLAlchemy ORM (Python), and I cannot find a way to force SQLAlchemy to add the cast suffix to NULL fields (:: timestamp). Not using SQLAlchemy is not an option right now.

. , "multi" text [], NULL- ( ).

, Postgres NULL.

, - :

  • SQLAlchemy ORM NULL/None ;
  • Postgres , NULL - :: timestamp? (, ?)

: , , , . , JSON ( PostgreSQL).

do , , , , DB Postgres, . .

, /, (, ). , , .

. !

+1

Source: https://habr.com/ru/post/1532332/


All Articles