1

I'd like to store Python dicts containing lists as HSTORE object in a PostgreSQL database using SQLAlchemy. Following my table class.

from sqlalchemy.dialects.postgresql import HSTORE
from sqlalchemy import Column, String
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class Data(Base):
    id = Column(String, primary_key=True)
    data = Column(HSTORE)

I insert my items in the following matter.

data = Data(
    id='fancy_id',
    data={
        'foo': ['bar', 'bar']
    },
)

db.Session.add(scan)
db.Session.commit()

This causes a ROLLBACK to happen and the following exception raises.

sqlalchemy.exc.DataError: (psycopg2.errors.ArraySubscriptError) wrong number of array subscripts

[SQL: INSERT INTO data (id, data) VALUES (%(id)s, %(data)s)]
[parameters: {'id': 'fancy_id', 'data': {'foo': ['bar', 'bar']}}]

However, the insertion works without the list.

data = Data(
    id='fancy_id',
    data={
        'foo': 'bar'
    },
)

I followed the PostgreSQL SQLAlchemy 1.4 documentation and can't spot a note indicating this limitation. Am I missing something? How can I store Python lists inside PostgreSQL HSTORE objects via SQLAlchemy?

Thanks in advance.

1 Answer 1

4

Honestly for this sort of thing json/jsonb would be a better option. Then you would have a direct mapping of Python dict with list to JSON object with array. If you want to use hstore then:

create table hstore_test(id int, hs_fld hstore);
insert into hstore_test values (1, 'a=>1');
insert into hstore_test values (1, 'b=>"[1,2]"'::hstore);
select * from hstore_test ;
 id |    hs_fld    
----+--------------
  1 | "a"=>"1"
  1 | "b"=>"[1,2]"

--So for your example

data={
        'foo': "['bar', 'bar']"
    },

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.