pallets-eco / flask-session

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

Table sessions is already defined #131

Closed paulbaumgarten closed 8 months ago

paulbaumgarten commented 3 years ago

Hello I have a project that was previously just using flask-session type filesystem which I am finally migrating over to sqlalchemy, but I've hit a snag.

# Database initialise
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URI']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
db.Model.metadata.reflect(db.engine)
# Session initialise
app.config['SESSION_TYPE'] = 'sqlalchemy'
app.config['SESSION_SQLALCHEMY_TABLE'] = 'sessions'
app.config['SESSION_SQLALCHEMY'] = db
session = Session(app)

This is throwing exception 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..

I realise this is likely due to the fact that I am using the db,Model.metadata.reflect option. Is there a way I can either (1) have sqlaclemy reflect all tables except sessions, or (2) have flask-session not define it's own model? or (3) Any other creative fix?

Many thanks

paulbaumgarten commented 3 years ago

For the benefit of anyone else who comes across this post in the future, I just forked this repo and modified the code so that the database model was not loaded if an optional flag was passed to the constructor function. I was actually a little surprised when I bothered to look at the code at just how small this library was - very easy to fix myself. Pity others have commented that the repo itself seems unmaintained now.

Lxstr commented 8 months ago

Fixed from 0.6.0