I'm using consecutive merge() calls to fetch the same persistent
instance from the Session.
The test case below works as I expected: the first get_item() issues a
SELECT, the second issues an UPDATE, the third does neither.

However, in my large project, using the pattern below (without the
attribute modification) seems to issue a SELECT for every call to
merge(). I'm trying to diagnose why this is happening but can't see
anything obviously wrong like committing between calls or anything.

In what circumstances are instances removed from the Session's
identity map? Is there somewhere I can read more about the internals
of this so I can gain a better understanding of the Session?

---

from sqlalchemy import create_engine, Column
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.types import Integer, String

engine = create_engine('sqlite:///:memory:', echo=True)
Base = declarative_base(bind=engine)
Session = scoped_session(sessionmaker(bind=engine))

class Item(Base):
    __tablename__ = 'items'
    id = Column(Integer, primary_key=True)
    name =  Column(String(50))

Base.metadata.create_all()

i = Item(id=1, name='Foo')
Session.add(i)

Session.commit()

def get_item():
    return Session.merge(Item(id=1))

one = get_item()
print one.name

one.name = 'Bar'

two = get_item()
print two.name

three = get_item()
print three.name

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to