2

This is a simplified version of my table:

class Alert(Base):
    __tablename__ = 'alert'

    id = Column(Integer, primary_key=True)
    created_on = Column(DateTime(timezone=True))
    category = Column(Unicode, nullable=False)
    parameters = Column(ARRAY(Unicode), nullable=True)

When trying to insert the following multidimensional array into the table, I get a DataError telling me that my array is malformed.

DataError: (DataError) malformed array literal: "numbers"
LINE 1: ...RRAY[ARRAY['key', 'sXl5SoNh0KY'], ARRAY['nu...
                                                   ^
DETAIL:  Array value must start with "{" or dimension information.
'INSERT INTO alert (created_on, category, parameters) 
VALUES (%(created_on)s, %(category)s, %(parameters)s) 
RETURNING alert.id' {
'created_on': datetime.datetime(2015, 6, 24, 1, 52, 30, 631330, tzinfo=<UTC>)
'category': u'new_cases',
'parameters':
    [[u'key', u'sXl5SoNh0KY'],
    ['numbers', [u'8129431', u'8669290', u'8754131', u'8871813', u'8927606']],
    ['status', 'all']]
}

I was under the impression that Postgres' ARRAY type is multidimensional regardless of how it is created (http://thread.gmane.org/gmane.comp.python.sqlalchemy.user/31186), so why is this error occurring?

1 Answer 1

4

Arrays in PostgreSQL can be multidimensional, but all the elements must have the same number of dimensions (http://www.postgresql.org/docs/9.4/interactive/arrays.html), so you can write:

SELECT ARRAY[ARRAY[1,2], ARRAY[3,4]];

=> "{{1,2},{3,4}}"

but not:

SELECT ARRAY[ARRAY[1,2], ARRAY[3]]

ERROR:  multidimensional arrays must have array expressions with matching dimensions
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.