kvesteri / sqlalchemy-continuum

Versioning extension for SQLAlchemy.
BSD 3-Clause "New" or "Revised" License
575 stars 127 forks source link

Using session.add() in execution stage of flush warning #294

Open AbdealiLoKo opened 2 years ago

AbdealiLoKo commented 2 years ago

I got a part of my code to use sqlalchemy-continuum after a figuring out a bunch of stuff When I ran my unit tests, I got this warning 800+ times:

00:21:29.767   /io/venv/lib/python3.6/site-packages/sqlalchemy_continuum/unit_of_work.py:123: SAWarning:
00:21:29.767   
00:21:29.767   Usage of the 'Session.add()' operation is not currently supported within the execution stage of the flush process. Results may not be consistent.  Consider using alternative event listeners or connection-level operations instead.

On checking, it seems this warning has been there in sqlalchemy since the past 10+ years (since sqlalchemy 0.8) https://github.com/sqlalchemy/sqlalchemy/commit/1c3e3225521647cc843a633e34ed84e1ca4e797aj

I'm wondering if I am using sqla-continuum wrongly ? Or is it expected ?

marksteward commented 2 years ago

This should only happen before flush, outside a transaction:

    def process_before_flush(self, session):
...
        if not self.current_transaction:
            self.create_transaction(session)
...
        session.add(self.current_transaction)

If you're seeing this message, I suspect something is incorrectly changing the state of the transaction, perhaps re-entry of some kind. Can you create a minimal complete reproduction for this warning?

AbdealiLoKo commented 2 years ago

Yep. Will try to create a example