Is there any way of avoiding associated objects staying loaded in memory after they have been added to a parent object through a
Example: given the
class Container has a one-to-many relationship to
Base = declarative_base() class Container(Base): id = Column(Integer, primary_key=True) items = relationship('Item') class Item(Base): container = Column(Integer(), ForeignKey('container.id'), nullable=False)
I can observe linear growth in memory when
item instances are added to a
container in a loop. It makes not difference in which direction the association is made:
# alt.1 for x in lots_of_data(): item = Item(x) item.container_id = container.id
# alt.2 for x in lots_of_data(): container.items.append(Item('foo'))
act the same. I tried
session.expunge and whatnot. Passing
lazy='dynamic' keeps memory down a bit, but the instead the processing take three times as long(!).
The only method I found of keeping it fast and lean, was to comment out the
items = relationship above, but then I loose functionality which I need later.
(I'm using Python 2.6.6, SQLAlchemy 0.7.2)