AttributeError: can't set attribute when connecting to sqlite database with flask-sqlalchemy

Edit

If you're experiencing this, upgrading Flask-SQLAlchemy to >= 2.5 should resolve the issue per https://github.com/pallets/flask-sqlalchemy/issues/910#issuecomment-802098285.

Pinning SQLAlchemy to ~1.3 should no longer be necessary.


I ran into this issue a little earlier, but think I've figured out what's going on.

SQLAlchemy is automatically installed as a dependency for Flask-SQLAlchemy and its latest release (1.4.0) introduces the following breaking change:

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.

I was able to fix this issue by simply installing the previous version of SQL Alchemy (1.3.23).


Double check that this issue affects you by running

pip freeze

You should find that the current version of sqlalchemy is 1.4.0. I found the quickest solution for now is to manually revert to a previous version of sqlalchemy:

pip install SQLAlchemy==1.3.23

If you've just reverted to the previous version and it works then now is a great time to pin your versions:

pip freeze > requirements.txt