On Jun 17, 2010, at 6:21 AM, Harry Percival wrote: > Hey all, > > I have some code that attempts to add a bunch of new entries to a table in a > single commit, with some try/excepts designed to catch any primary key > errors. if any errors occur, i want to be able to manually go through and > re-attempt each line one by one to understand which ones fail and which > don't, and to make sure all the legal ones get done. > > def add_rows_to_table(self,all_rows): > Session = sessionmaker(bind=self.engine) > session = Session() > for row in all_rows: > item = MyClass(row) > session.add(item) > try: > session.commit() > except Exception,e: > #roll back, start new session, add rows one by one, raise more > specific error > print '='*10,'\nDEBUG-- encountered error, attempting > one-at-a-time commits' > print 'database contents as follows > %s'%str(MyTable.get_table_as_columns_dict()) > session.rollback() > session.close() > for rowno, row in enumerate(all_rows): > #commit rows one at a time > etc etc > > > However, I'm getting some unexpected behaviour. When I do my initial > transaction which attempts to do all rows at the same time, it falls over as > expected if it runs into a PK clash and says it's doing a rollback.. BUT in > fact any of the successful rows before the pk clash seem to still be in the > db, ie they were'nt rolled back. is this expected behaviour?
If the debug output below is from the "print" line above that says "database contents as follows", you haven't rolled back the transaction yet, which is suspcious here, but the underlying SQLite transaction is rolled back, so at that point the table is empty. The session is also unusable on that line so this makes it that much more mysterious what "get_table_as_columns_dict()" could possibly be doing. Since I don't know what "MyTable.get_table_as_columns_dict()" is or anything else like that, you'd have to illustrate a full test. I imitated your code above as closely as I could tell and it works fine. -- 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.
