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

Make Session table reusable #176

Closed nebolax closed 5 months ago

nebolax commented 1 year ago

Closes #175

If someone runs multiple tests and each test initializes a new instance of flask app (and thus a new initialization of flask_session happens), it results in the following 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.

This is because typically the sqlalchemy db object is global and thus it's metadata is kept between tests runs. Which means that if someone executes class SomeTable(db.Model) twice, it breaks because the table is already stored in the metadata.

This PR solves this problem by setting keep_existingto True, which means that if sessions table is already in the metadata, it will be reused instead of trying to create a new one.

A test was also added for the mentioned change.

nebolax commented 1 year ago

@fengsp @davidism @rayluo I am looking forward for a review on this PR!

Lxstr commented 5 months ago

@nebolax This should be fixed in the latest version. Thanks for the PR, this demonstrated a cleaner fix than other PRs and it's coded it now directly in combination with some other items.

nebolax commented 3 months ago

Heyy @Lxstr great that it was fixed! Could you include me in the flask-session's contributors list?