flask-session icon indicating copy to clipboard operation
flask-session copied to clipboard

Can't reset sessions table between tests

Open nebolax opened this issue 2 years ago • 6 comments

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?

nebolax avatar May 24 '23 11:05 nebolax

@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.

christopherpickering avatar May 24 '23 12:05 christopherpickering

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 avatar May 25 '23 05:05 nebolax

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 avatar May 25 '23 06:05 nebolax

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

nebolax avatar May 25 '23 06:05 nebolax

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

nebolax avatar May 26 '23 13:05 nebolax

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

christopherpickering avatar May 26 '23 13:05 christopherpickering

Now fixed.

Lxstr avatar Mar 11 '24 08:03 Lxstr