Closed fadyZohdy closed 2 years ago
Yeah, this looks like a bug. I think you can work around it by moving the call to sessionmaker
(and create_engine
) to before the call to make_versioned
.
I'll need to dig more into sqlalchemy's event logic to create a fix.
We are calling make_versioned
after calling sessionmaker
. But facing this issue intermittently.
In my case I have to call make_versioned()
before each model class I __versioned__
.
And DB session initialization part looks like this:
configure_mappers()
engine = create_engine(
settings.SQLALCHEMY_DATABASE_URI,
pool_pre_ping=True,
# echo=True
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
make_versioned(plugins=[UserTrack()])
@event.listens_for(SessionLocal, "before_flush")
...
My UserTrack
it's to get User.id
from request headers.
Anyway, I still get a warning in the console:
lib/python3.9/site-packages/sqlalchemy_continuum/unit_of_work.py:260: SAWarning: implicitly coercing SELECT object to scalar subquery; please use the .scalar_subquery() method to produce a scalar subquery.
getattr(
I am facing a similar issue - but not from event listener - but when I run my alembic migrations. Not sure if I should be opening a separate ticket for that or it should be a part of this same ticket.
It looks like when I run alembic upgrade
it throws this same KeyError at this same point.
Also foud a similar ticket: https://github.com/kvesteri/sqlalchemy-continuum/issues/187
Spent some time on this. And seems like @marksteward was right - this is about the event management with sqlalchemy. I have raised a discussion on sqlalchemy asking for the correct way to register listeners on some events https://github.com/sqlalchemy/sqlalchemy/discussions/8466
This was confirmed to be a bug in sqlalchemy. This has been fixed in: https://github.com/sqlalchemy/sqlalchemy/issues/8467 So, we just have to wait for the next release.
As this is not a bug in sqlalchemy-continuum - shall we close this ticket @marksteward ?
sqlalchemy 1.4.41 was released yesterday - when I try the original example with this new version it works without any errors
venv/bin/pip install -U sqlalchemy==1.4.40
python test.py # Throws the KeyError
venv/bin/pip install -U sqlalchemy==1.4.41
python test.py # No errors !!
So, this ticket can be closed ?
Yep, thanks @AbdealiJK!
we use sessionmaker in our app and i have been getting this error when i listen to the before_flush event with sessionmaker object
this error can be reproduced by doing a small modification to the example in README
deps versions:
my experience with sqlalchemy is not extensive and i tried debugging it but couldn't get anywhere. i am ready to help fix this but would use a little guidance.