Problem while committing data using SQLAlchemy Session object in a Loop?

You might try restructuring your code to use this: Session = sqlalchemy.orm. Sessionmaker(...) def transaction(self, callback): session = sqlalchemy.orm. Scoped_session(Session) try: result = callback(session) except: session.rollback() raise else: session.commit() finally: session.close() return result Then each transaction goes in its own function, like so: def updatetxn(pk, newvalue): def txn(session): obj = session.

Query(myclass). Filter_by(id=pk).one() obj. Field = newvalue session.

Add(obj) return txn transaction(updatetxn(4, 'abc')) Handling the commit/rollback logic in one place, and relying on function scope to scope a unit of work, may reduce the complexity of your application and eliminate bugs you haven't found yet.

There's a very good chance that you don't need to issue two commits; One of the main reasons for using sqlalchemy sessions is that it understands how objects can interrelate, and will order its inserts in such a way that data gets properly inserted and correctly represents the desired structure. This works principally through the relationship construct. Here's a simple example: >>> from sqlalchemy import * >>> from sqlalchemy.

Orm import * >>> from sqlalchemy.ext. Declarative import declarative_base >>> Base = declarative_base() >>> class A(Base): ... __tablename__ = "a_table" ... id = Column(Integer, primary_key=True) ... >>> class B(Base): ... __tablename__ = "b_table" ... id = Column(Integer, primary_key=True) ... a_id = Column(Integer, ForeignKey(A. Id)) ... a = relationship(A) ... >>> my_a = A() >>> my_b = B() >>> my_b.

A = my_a >>> The most important part is that we are declaring a relationship between A and B through B.a. To take best advantage of this, it's important to express the relationship between instances of each through this relationship property, and let sqlalchemy take care of setting the a_id column itself. >>> engine = create_engine("sqlite:///:memory:") >>> Base.metadata.

Create_all(engine) >>> engine. Echo = True >>> Session = sessionmaker(engine) >>> >>> session = Session() >>> session. Add(my_a) >>> session.

Add(my_b) >>> >>> session.commit() With echo=True, the output looks a bit like this: 2011-09-16 17:19:22,367 INFO sqlalchemy.engine.base.Engine.0x...ed50 BEGIN (implicit) 2011-09-16 17:19:22,368 INFO sqlalchemy.engine.base.Engine.0x...ed50 INSERT INTO a_table DEFAULT VALUES 2011-09-16 17:19:22,368 INFO sqlalchemy.engine.base.Engine.0x...ed50 () 2011-09-16 17:19:22,369 INFO sqlalchemy.engine.base.Engine.0x...ed50 INSERT INTO b_table (a_id) VALUES (?) 2011-09-16 17:19:22,369 INFO sqlalchemy.engine.base.Engine.0x...ed50 (1,) 2011-09-16 17:19:22,369 INFO sqlalchemy.engine.base.Engine.0x...ed50 COMMIT Notice that the my_a object is inserted, and sqlalchemy reads the assigned primary key and uses that for the insert for my_b.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions