[Sqlalchemy-tickets] Issue #3577: get_history() call on relationship may interfere with other attri
Brought to you by:
zzzeek
From: Roman Z. <iss...@bi...> - 2015-11-05 14:13:26
|
New issue 3577: get_history() call on relationship may interfere with other attributes' histories https://bitbucket.org/zzzeek/sqlalchemy/issues/3577/get_history-call-on-relationship-may Roman Zimmermann: I currently try to get the original values of certain attributes in `before_commit`. For that I call `get_history` on some attributes. It seems that the history of all attributes is flushed, when the history of the relationship is loaded. Here is a full test-script. ``` #!python from sqlalchemy import create_engine, Column, String, Integer from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import backref, relationship, sessionmaker from sqlalchemy.orm.attributes import get_history, History from sqlalchemy.sql.schema import ForeignKey Base = declarative_base() class User(Base): __tablename__ = 'user' id = Column(Integer, primary_key=True) name = Column(String) class Tag(Base): __tablename__ = 'tag' id = Column(Integer, primary_key=True) user_id = Column(ForeignKey(User.id)) tag = Column(String) user = relationship(User, backref=backref('tags')) engine = create_engine('sqlite:///:memory:', echo=True) Base.metadata.create_all(engine) session = sessionmaker(bind=engine)() u = User(name='old') session.add(u) session.commit() getattr(u, 'name') u.name = 'new' assert get_history(u, 'name') == History(['new'], (), ['old']) get_history(u, 'tags') # Fails: History(added=(), unchanged=['new'], deleted=()) assert get_history(u, 'name') == History(['new'], (), ['old']) ``` It seems that the call `get_history(u, 'tags')` leads to the update being flushed. Is there any way to consistently get the original value of an attribute from before the transaction has started? This was tested using 1.0.8. |