Quick question on HStore et al. How is it comparing to Json in 9.3? And how does sqlalchemy handle HSTORe
I am going to make the leap and put some experimental schemea-less designs into pg soon and would appreciate any feedback from the trenches !
I am going to make the leap and put some experimental schemea-less designs into pg soon and would appreciate any feedback from the trenches !
Usage:
You have to tell Postgres to use the HSTORE extension for your database. It's shipped with 9.3 but not active by default.
Then for SQLAlchemy you'd add something like this in your models.py:
from sqlalchemy.dialects.postgresql import HSTORE
from sqlalchemy.ext.mutable import MutableDict
class Widget(db.Model):
id = db.Column(db.Integer, primary_key=True)
data = db.Column(MutableDict.as_mutable(HSTORE))
You don't technically have to do the MutableDict stuff to use HSTORE, but it's handy. They're mutable on the Python side already, so why not be consistent. With the rest of the model as appropriate (init, repr), and I'm using Flask-SQLAlchemy which makes the contexts line up and such.It's nice because it's an RDB field so you can do all the usual Postgres love when you're not wanting to store a dict(). Or you can just do a table with id, data, and id can even be a kv-style key if you want some sort of meaning attached to the primkey. Plus it's dicts, and Python is heavy on the dicts, and it's a quick leap from there to JSON.
thank you