Skip to content

Instantly share code, notes, and snippets.

@plaes
Created December 13, 2012 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plaes/4277752 to your computer and use it in GitHub Desktop.
Save plaes/4277752 to your computer and use it in GitHub Desktop.
Bug in sqla hstore mutability implementation
from sqlalchemy import create_engine
from sqlalchemy import Column, Integer
from sqlalchemy.dialects.postgres import HSTORE
from sqlalchemy.sql import text
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('postgresql:///hstore_test', echo=True)
session = sessionmaker(bind=engine)()
Base = declarative_base()
class C(Base):
__tablename__ = 'foo'
id = Column(Integer, primary_key=True)
hashes = Column(HSTORE, server_default=text("''::hstore"))
def __init__(self):
self.hashes = {}
def __repr__(self):
return '%s: %s' % (self.id, self.hashes)
Base.metadata.create_all(engine)
session.query(C).delete()
a = C()
a.hashes["foo"] = '123'
b = C()
b.hashes = {'foo': '123'}
session.add_all([a, b])
session.commit()
a, b = session.query(C).all()
print a.hashes['foo'], b.hashes['foo']
a.hashes['foo'] = "123456"
b.hashes = {"foo": '123456'}
session.commit()
a, b = session.query(C).all()
assert a.hashes['foo'] == b.hashes['foo']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment