1

I want to create a table in my SQLAlchemy Pyramid app that stores a multidimensional array of values. I'm using PostgreSQL and I know that it has an array type. I'm just not quite sure of how to define the table in my models.py. Maybe something like:

class MyTable:
    __tablename__ = 'mytable'
    id = Column(Integer, primary_key=True)
    text_array = Column([Unicode])

    def __init__(self, text_array):
        self.text_array = text_array
1
  • If it's variable length and depth, prefer json. It'll be more straightforward to serialize and unserialize. And there's a json type which is useful in 9.3. Commented Oct 17, 2013 at 17:13

1 Answer 1

4

According to the SQLAlchemy docs, you use the dialect-specific ARRAY type. Modifying your example leads to:

from sqlalchemy.dialects.postgresql import ARRAY

class MyTable:
    __tablename__ = 'mytable'

    id = Column(Integer, primary_key=True)
    text_array = Column(ARRAY(Unicode))

    def __init__(self, text_array):
        self.text_array = text_array

See the docs for multi-dimensional arrays (simply add the "dimensions" keyword argument to the ARRAY constructor).

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.