I use Flask-SQLAlchemy with Celery. The two play poorly together if the Celery task takes a long time, as when it is done when the commit occurs, the MySQL connection will have timed out and "gone away".
Is it possible to make changes to a SQLAlchemy object, attempt a commit, and when that fails, open a new session, attach the objects to the new session, and commit them? If so, how? What kind of SQLAlchemy function can do this? Or now that the commit failed as the session is gone, are the SQLAlchemy objects invalidated and all the work on them must be done again?
The answer is merge. merge is what can be used to attach objects to different sessions.
How to recover from a failed SQLAlchemy commit?