Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

Im working with multidimensional arrays, and I have noticed that postgres needs casted expression to insert values, eg.:

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 is doing when I add an object, it doesn't make 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 at 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 makes the type casting also when values within list are None? Perhaps some custom type implementation ?

share|improve this question

1 Answer 1

It looks like you are trying to insert

VALUES (ARRAY[NULL])

which it is reading as

VALUES ('ARRAY[NULL]')

while what you actually want to insert is

VALUES ({NULL})

or

VALUES ({})

i.e. an empty array.

Check out the PostgreSQL manual's page on arrays for more details about array behaviour and related examples

share|improve this answer
    
No, i do want to store {NULL}, but the question here is not how to do that in postgres, but on sqlalchemy –  user1538560 Sep 18 '14 at 16:47
    
Ok, I was misunderstanding a bit, sorry. You are trying to send it [none] which is a text string, but if you give it [] which is a null string instead, that might work. –  Sam Sep 19 '14 at 23:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.