Created
December 13, 2012 16:37
-
-
Save plaes/4277752 to your computer and use it in GitHub Desktop.
Bug in sqla hstore mutability implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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