Trying next code: ------------------------- try: session.begin(subtransactions=True) session.add(importe) session.flush() nimportados += 1 except Exception as ex: log.debug(ex) session.rollback() nerroneos += 1 -------------------------
I can see into the log: ------------------------- 12:55:45,598 INFO [sqlalchemy.engine.base.Engine.0x...f1ac] BEGIN 12:55:45,600 INFO [sqlalchemy.engine.base.Engine.0x...f1ac] INSERT INTO importes (tarifas_id, prefijo, destino, importe, tipo, pc, psi, ps, sg) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s) 12:55:45,600 INFO [sqlalchemy.engine.base.Engine.0x...f1ac] [u'1', 'PREFIJO', 'DESTINO', 'IMPORTE', u'', 0, 60, 60, 0] 12:55:45,601 INFO [sqlalchemy.engine.base.Engine.0x...f1ac] ROLLBACK 12:55:45,603 DEBUG [svbpanel.controllers] (IntegrityError) (1062, "Duplicate entry 'PREFIJO' for key 'prefijo'") u'INSERT INTO importes (tarifas_id, prefijo, destino, importe, tipo, pc, psi, ps, sg) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)' [u'1', 'PREFIJO', 'DESTINO', 'IMPORTE', u'', 0, 60, 60, 0] 12:55:45,603 DEBUG [svbpanel.controllers] The transaction is inactive due to a rollback in a subtransaction. Issue rollback() to cancel the transaction. ------------------------- and "InvalidRequestError: The transaction is closed" into TG web debug messages. It seems to auto run the ROLLBACK but. Any idea? On 13 oct, 15:00, "Diez B. Roggisch" <[email protected]> wrote: > On Wednesday, October 13, 2010 14:43:31 Juan Antonio Ibáñez wrote: > > > > > > > Hello! > > > I am trying to do some INSERT to my DB from one controller method. I > > do following try/except inside a loop: > > > try: > > DBSession.add(importe) > > DBSession.flush() > > nimportados = nimportados + 1 > > except Exception as ex: > > nerroneos = nerroneos + 1 > > > I need to check how many inserts are ok and how many with error. Using > > previous code I get following message when an exception occurs: > > > InvalidRequestError: The transaction is inactive due to a rollback in > > a subtransaction. Issue rollback() to cancel the transaction. > > > Is there any way to force insert for each add() call and to control > > No. Use sub-transactions for that. I had that discussion with Michael Bayer > before. He's adamant about this. > > Diez -- You received this message because you are subscribed to the Google Groups "TurboGears" 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/turbogears?hl=en.

