Search code examples
pythonpostgresqlhstore

How to store a python dict in Postgresql hstore


I would like to store this kind of dict in hstore. Is it supported by hstore?

dict= {'fieldnames':['type', 'precision', 'width', 'data']}

If not, is there a way to flatten that structure to make it supported by hstore?


Solution

  • What you'd want to do is write a wrapper on the field that will stringify nested data on writes and the de-serialize that info when you read it back.

    e.g. you want to store it like this:

    {"fieldnames" : "['type', 'precision', 'width', 'data']"}
    

    you can do it using json.dumps:

    {"fieldnames" : json.dumps(['type', 'precision', 'width', 'data'])}