Python SqlAlchemy insert ARRAY (postgres) with NULL values ​​impossible?

Im works with multidimensional arrays, and I noticed that postgres needs a cast expression to insert values, for example:

CREATE TABLE test (
    pay  integer[]
);
CREATE TABLE

INSERT INTO test values (ARRAY[NULL]);
ERROR:  column "pay" is of type integer[] but expression is of type text[]
LINE 1: INSERT INTO test values (ARRAY[NULL]);
                                 ^
HINT:  You will need to rewrite or cast the expression.

INSERT INTO test values (ARRAY[NULL::integer]); -- How to do this on SqlAlchemy ?
INSERT 0 1                                      -- ARRAY[NULL]::integer[] would also works

This is what SqlAlchemy does when I add an object, it does not do type casting if VALUE is NULL. Here is part of my code:

from sqlalchemy.dialects.postgresql import ARRAY
class Test (Base):

    __tablename__ = 'test'

    pay = Column(ARRAY(Integer))

member = Test()
member.pay = [None]
session.add(member)
session.commit()

And then in postgres log I get:

ERROR:  column "pay" is of type integer[] but expression is of type text[] at character 26
HINT:  You will need to rewrite or cast the expression.
STATEMENT:  INSERT INTO test (pay) VALUES (ARRAY[NULL]);  -- See ?? No casting from SqlAlchemy

So the question is: what can I do for SqlAlchemy, does type listing also when there are no values ​​in the list? Perhaps some kind of implementation of a non-standard type?

+4
source share
2 answers

It seems to be an omission in psycopg2; update from 2.4.6 to 2.6.2 fixed. Thanks of course to the author of SQLA @zzzeek

0

,

(ARRAY [NULL])

('ARRAY [NULL]')

({NULL})

({})

. .

PostgreSQL

0

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


All Articles