usually some kind of collection of objects you want to keep is maintained, it can be passed throughout those methods, or it can be attached to the Session. It's a tradeoff between which objects you'd like to keep around between calls to things, and which you'd like to be garbage collected.
On May 22, 2013, at 3:49 AM, Etienne Rouxel <[email protected]> wrote: > My real application seems to share the cause for why the identity map is not > used. Indeed, I have a method that encapsulate the building of a form using > queries. Once the execution goes out of this method, the instances are > cleared out from the identity map. > I guess this is a very common concern, is there any appropriate design > pattern to fully take advantage of the identity map? > > Le mardi 21 mai 2013 17:31:10 UTC+2, Etienne Rouxel a écrit : > Hello > In my program, I was trying to guess why so many SQL queries were sent while > some could have been avoided with the help of the identity map. > So, I reduced my program to what is below and wrote 3 times the same > query.get call and 3 SQL queries were sent to the database server. > Why does SQLAlchemy send 3 times the SQL while it could have done it only 1 > time for the first call and use the identity map for the 2 last calls? > > from sqlalchemy import * > from sqlalchemy.orm import * > from sqlalchemy.ext.declarative import declarative_base > Base = declarative_base() > > _descriptiontype_table = Table('descriptiontype', Base.metadata, > Column('id', Integer, primary_key=True), > Column('refno', Integer), > Column('sortindex', Integer), > Column('designation', String), > schema='botany' > ) > > class Descriptiontype(Base): > __table__ = _descriptiontype_table > > if __name__ == '__main__': > > engine = create_engine('postgresql://user@localhost:5432/mydatabase') > Session = sessionmaker(bind=engine) > session = Session() > > session.query(Descriptiontype).get(-2147483648) > session.query(Descriptiontype).get(-2147483648) > session.query(Descriptiontype).get(-2147483648) > > > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
