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]);
INSERT 0 1
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]);
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?
source
share