. Advertisement .
..3..
. Advertisement .
..4..
A common mistake new Python coders make is to run into the error “AttributeError: can’t set attribute when connecting to sqlite database with flask-sqlalchemy” during their first Flask based application. This blog will explain 3 popular ways on how to fix this error.
How To Fix The Error “AttributeError: can’t set attribute when connecting to sqlite database with flask-sqlalchemy”?
Your Flask project was running smoothly the day before. Yet, one day, when running your project, you might receive an error. When looking at your code, you cannot see anything wrong. It also appears to be similar to the code of the working project, so you’re not sure what I’m missing.
Here is the issue:
(env) PS C:\my-projects\task-master-tdd> flask shell
Python 3.8.5 (tags/v3.8.5:580fbb0, March 20 2021, 10:04:08) [MSC v.1926 32 bit (Intel)] on win32
App: project [development]
Instance: C:\my-projects\task-master-tdd\instance
>>> from project import db
>>> db
Traceback (most recent call last):
AttributeError: can't set attribute
>>>
SQLAlchemy is installed as a dependency for Flask-SQLAlchemy by default, and the most recent release (1.4.0) includes the following breaking modification. To fix this, simply downgrade the version. Downgrade by following the steps below.
Option 1: DownGrade your sqlalchemy version
To fix this, simply downgrade the version. Downgrade by following the steps below.
1. By running pip freeze, double-check that this problem affects you.
2. Sqlalchemy 1.4.0 is the most recent version.
3. For the time being, we’ve found that manually reverting to an earlier version of sqlalchemy is the best approach:
pip install SQLAlchemy==1.3.23
4. That is how to fix it.
Option 2:
1 Simply freeze the packages.
requirements.txt > pip freeze
2. In the requirements.txt, upgrade your SQLAlchemy.
SQLAlchemy<1.4.0
3. Install the packages again.
requirements.txt pip install -r
Option 3:
We’ve just looked into this error and discovered that SQLAlchemy is being updated automatically, which is causing the problem.
SQLAlchemy is installed as a dependency for Flask-SQLAlchemy by default, and the most recent release (1.4.0) includes the following breaking modification:
The URL object is now an immutable named tuple. To modify a URL object, use the URL.set() method to produce a new URL object.
You can resolve this problem by reinstalling SQL Alchemy’s earlier version (1.3.23).
Conclusion
We hope you found our article on fixing the error “AttributeError: can’t set attribute when connecting to sqlite database with flask-sqlalchemy” useful. We understand that dealing with this can be confusing, therefore we hope that our information has assisted you in resolving it. Please leave a comment if you have any further questions or concerns. Thank you for taking the time to read; we are always delighted anytime one of our pieces can give important information on a topic like this!
Leave a comment