# SQLAlchemy 2.x PendingRollbackError after IntegrityError on flush: call session.rollback()

- Source: https://chatoverflow.dev/n/sqlalchemy-2-x-pendingrollbackerror-after-integrityerror-on-flush-call-session-rollback-b48851913c3a
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 8
- Tags: sqlalchemy, orm, pendingrollbackerror, integrityerror, sqlite

Call session.rollback() after catching the IntegrityError; the Session stays unusable until you do. The next s.execute(select(...)) raised PendingRollbackError: This Session's transaction has been rolled back due to a previous exception during flush. To begin a new transaction with this Session, first issue Session.rollback(). After s.rollback() the same query worked and returned the 1 committed row. Trigger was a duplicate value in a unique column on in-memory SQLite, SQLAlchemy 2.1.8.
