behai-nguyen / behai-nguyen.github.io

My GitHub page.
1 stars 0 forks source link

2022/11/24/flask-session-sessions-exists-exception #5

Open utterances-bot opened 6 months ago

utterances-bot commented 6 months ago

Python: a workaround for SQLAlchemy “Table ‘sessions’ is already defined” exception during tests. | behai-nguyen software development learnings and documentation

During tests, Flask-Session intermittently causes the 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’m presenting a workaround in this post.

https://behai-nguyen.github.io/2022/11/24/flask-session-sessions-exists-exception.html

behai-nguyen commented 6 months ago

Hi @lxstr,

First, thank you for your hard works. And do apologise for the late reply. I had been caught up in something else.

I have today just finished testing out Flask-Session new versions.

Following are what I have found out:

On Tests

This is the one that occurs using version 0.4.0:

E               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 the new one, which did not happen on version version 0.4.0:

ERROR tests/bro/test_rbac_bro.py::test_list_all_system_menu - AttributeError: 'SqlAlchemySessionInterface' object has no attribute 'db'
ERROR tests/unit/test_rbac_system_menu.py::test_sqlalchemy_filter_id_equal_01 - sqlalchemy.exc.InvalidRequestError: Table 'sessions' is already defined for this MetaData instance.  Specify 'exten...
ERROR tests/unit/test_rbac_system_menu.py::test_sqlalchemy_filter_id_equal_02 - sqlalchemy.exc.InvalidRequestError: Table 'sessions' is already defined for this MetaData instance.  Specify 'exten...

The Application

This is the new one, which did not happen on version version 0.4.0:

...
  File "E:\book-keeping\app.py", line 16, in <module>
    app = create_app()
  File "e:\book-keeping\src\book_keeping\__init__.py", line 45, in create_app
    init_app_session( app )
  File "e:\book-keeping\src\book_keeping\__init__.py", line 69, in init_app_session
    app_session.app.session_interface.db.create_all()
AttributeError: 'SqlAlchemySessionInterface' object has no attribute 'db'

Please note, I did also verify that version 0.4.0 causes error as has been described in post.

Thank you and best regards,

...behai.