One way that a flush occurs is when you call query. Calling query on a model flushes the pending changes and adds them to the session so that when you call a .all() or .first(), those pending changes get translated into SQL statements.
- A flush takes pending changes, and translates them into commands ready to be committed. It occurs.
- when you call Query. Or on db.session.commit()
A commit leads to persisted changes on the database + lets the db.session start with a new transaction.
When a statement has been flushed already, SQLAlchemy knows not to do the work again of translating actions to SQL statements.