During tests, Flask-Session intermittently causes the exception “sqlalchemy.exc.InvalidRequestError: Table 'sessions' is already defined for this M...
For further actions, you may consider blocking this person and/or reporting abuse
Hi there is a recent fix for this in 0.6.0. Please let me know if that works for you
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:
Version 0.6.0 works as you have stated. I have been able to run tests multiple without the stated error occured.
Version 0.8.0 is worse than version 0.4.0, it actually raises 3 ( three ) errors:
On Tests
This is the one that occurs using version 0.4.0:
This is the new one, which did not happen on version version 0.4.0:
The Application
This is the new one, which did not happen on version version 0.4.0:
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.
Hi Behai, there were some changes from 0.6 to 0.8 but they 'should' have been improvements. From the errors I'm seeing it looks as if you are trying to subclass/override the interface and/or create tables or access the db attribute (which is internal and now renamed). There should ideally no longer be any need to do anything other than use the default interface, could you you confirm you are trying 0.8.0 without any such modifications or show me the specific lines the error occurs? Thanks!
Hi @lxstr,
Thank you for replying so quickly. What I did was verifying that "my errors" still occur using 0.4.0. And they do. Then I uninstalled 0.4.0, install 0.6.0, no errors occur for the tests or the application. Then I uninstalled 0.6.0, installed 0.8.0, then I got the three errors as reported in my previous reply, I ran it more than once, the errors were consistent. I then uninstalled 0.8.0, reinstalled 0.6.0 again. No errors, I ran everything more than once.
-- I tested both 0.6.0 and 0.8.0 with exactly the same code.
I have not looked into why the errors occur yet. I will do so in the next few days.
Thank you and best regards, Lex.
...behai.
Hi Lex,
My
E:\book-keeping\setup.py
, please noteFlask-Session=0.8.0
:My
E:\book-keeping\src\book_keeping\__init__.py
:Please note:
E:\book-keeping\src\book_keeping\library\
DOES NOT havefixed_session.py
module. I renamed it. Please see attached screenshot:My
E:\book-keeping\.env
:Full application error using version 0.8.0, "application" being
venv\Scripts\flask.exe run
:I can confirm that, after extracting the above error for this post, I went back to version 0.6.0, and everything works.
-- Please note that, to go back to version 0.6.0, I have to change the entry in
setup.py
to'Flask-Session=0.6.0',
, then manually install it with:Thank you and best regards,
...behai.
Hi Lex,
Just a little bit more info...
Regarding version 0.8.0, in
E:\book-keeping\src\book_keeping\__init__.py
, change to:from:
-- That is attribute
client
replaced attributedb
. The application runs.However, my test still results in error:
as per version .0.4.0.
Thank you and best regards,
...behai.
Hi Lex,
For version 0.8.0, in
sqlalchemy\sqlalchemy.py
, I copy the following from 0.6.0:to replace:
That is:
And errors disappear. The tests and the application work.
Thank you and best regards,
...behai.
Ok I may decide to add again the "keep_existing": True, however this shouldn't be needed. If you delete or comment the following lines and use 0.8.0, I would be interested to know the result. There should be no need to manually create the table as flask-session does this now.
and
with app.app_context():
Hi Lex,
Based on your advice:
E:\book-keeping\src\book_keeping\__init__.py
modified to:I have also decided to use latest of everything,
E:\book-keeping\setup.py
updated to:I am using the original
Flask-Session 0.8.0
:Test 1: table
sessions
exists:The application runs. That is,
venv\Scripts\flask.exe run
does not raise exception.Tests no longer raise the table
sessions
exists exception. They now raise another unrelated exception.Test 2: I manually removed the existing
sessions
table:The results are as in Test 1. BUT THE TABLE
sessions
DOES NOT GET CREATED.Further note:
With:
Tests raise the old exception (0.4.0). But the
sessions
table gets created.venv\Scripts\flask.exe run
no longer raises the old exception (0.4.0) and the tablesessions
table also gets created.Please note that, I posted this article on the 24th, November 2022. And labelled the code for this article. That is why I am able to get the code test this problem. I forgot to label database version!
Since the this article, I added more functionalities including database changes till around May 2023, I have since stopped working on this project.
For these tests, for the application (that is,
venv\Scripts\flask.exe run
), I can only get to the login page. But that should be enough.Please let me know if you want me to do anything else.
Thank you and best regards,
...behai.
Hi @lxstr,
Your advice works.
And this report from my last response is not correct:
There was something in my code what caused it not to work. I am using all latest packages now.
There is no need for:
I do apologise for all the fault alarms. I appreciate your helps.
Thank you and best regards,
...behai.
P.S.
Just a side note, my application has a view session functionality for admins, whereby it just displays existing sessions as JSON. I was using
pickle
to serialise the session data.Flask-Session 0.8.0
now usesmsgspec
, the serialisation ofsessions.data
is now:Hi Lex,
I am sorry for my late reply. Thank you very much. I will have a look at it. I have been working on some other things.
Thank you and best regards,
...behai.