pallets-eco / flask-session

Server side session extension for Flask
https://flask-session.readthedocs.io
BSD 3-Clause "New" or "Revised" License
490 stars 236 forks source link

Can't reset sessions table between tests #175

Closed nebolax closed 3 months ago

nebolax commented 1 year ago

I am trying to run tests on my app that uses flask_session and I use sqlalchemy to store sessions data (it gets stored in sessions table).

If I run tests one-by-one, everything is fine. But if I run multiple tests, then starting at the second test and on I am getting error sqlalchemy.exc.InvalidRequestError: Table 'sessions' is already defined for this MetaData instance. Specify 'extend_existing=True' to redefine options and columns on an existing Table object.. And I am getting it even if I reset all tables in the db before / after every test.

What am I doing wrong and how can I solve this?

christopherpickering commented 1 year ago

@nebolax for a short term fix, I think you need what is updated in pr #170 👍🏽 image

You could update a fork for your project or use flask-session2 until this gets fixed here.

nebolax commented 1 year ago

Thanks for the suggestion @christopherpickering ! Unfortunately this does not help since the error happens at the point of initializing the session db model (class Session(self.db.Model):)

nebolax commented 1 year ago

I was able to workaround this by executing db.metadata.clear() or db.metadata._remove_table('sessions', your_schema_name) before every Session initialization in my tests.

nebolax commented 1 year ago

@christopherpickering will it make sense to open a PR to fix this? In #170 this problem still exists

nebolax commented 1 year ago

Here is the pr #176. Looking forward for a review! @christopherpickering

christopherpickering commented 1 year ago

Looks good to me. I will let someone else review as well.

Lxstr commented 3 months ago

Now fixed.