Solve the Error “AttributeError: can’t set attribute when connecting to sqlite database with flask-sqlalchemy”

Python is a high-level, object-oriented, interactive, interpreted, multipurpose, and versatile scripting programming language. Programmers and developers are using Python for creating big data applications, mobile applications, desktop software, data science, automation, and even web pages. Programmers are creating a number of projects to make impactful applications. It is a widely used language that has an easy syntax. It is also used for the relational database using SQL. SQLAlchemy is known as a library to communicate between Python and databases. It is basically the Python SQL toolkit that offers SQL flexibility to programmers and developers.

SQLAlchemy is also used as an ORM tool (Object Relational Mapper) used to translate classes of Python to tables on databases. When you are working with SQLAlchemy, you may experience the error “AttributeError: can’t set attribute when connecting to sqlite database with flask-sqlalchemy”. Check out how the error occurs

How do you get the error?

When you are working on a flask project, you get the error warning. Check out the code that results in the error file

import os

# load the environment variables from the .env file
from dotenv import load_dotenv

# Determine the folder of the top-level directory of this project
BASEDIR = os.path.abspath(os.path.dirname(__file__))

class Config:
    FLASK_ENV = 'development'
    TESTING = False
    DEBUG = False
    SECRET_KEY = os.getenv('SECRET_KEY', default='A very terrible secret key.')
                                        default=f"sqlite:///{os.path.join(BASEDIR, 'instance', 'app.db')}")

class DevelopmentConfig(Config):
    DEBUG = True

class TestingConfig(Config):
    TESTING = True
                                        default=f"sqlite:///{os.path.join(BASEDIR, 'instance', 'test.db')}")

class ProductionConfig(Config):
    FLASK_ENV = 'production'

User model

from project import db, login_manager
from flask_login import UserMixin
from import generate_password_hash, check_password_hash

class User(db.Model, UserMixin):
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String, unique=True)
    hashed_password = db.Column(db.String)

    def __init__(self, username, password):
        self.username = username
        self.hashed_password = generate_password_hash(password)

    def is_password_valid(self, password):
        return check_password_hash(self.hashed_password, password)

    def __repr__(self):
        return '<User {}>'.format(

def load_user(user_id):
    return User.query.get(int(user_id))

This program shows the error warning. Have a look at it

(env) PS C:\coding-projects\task-master-tdd> flask shell
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:43:08) [MSC v.1926 32 bit (Intel)] on win32
App: project [development]
Instance: C:\coding-projects\task-master-tdd\instance
>>> from project import db
>>> db
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "c:\coding-projects\task-master-tdd\env\lib\site-packages\flask_sqlalchemy\", line 1060, in __repr__
    self.engine.url if or current_app else None
  File "c:\coding-projects\task-master-tdd\env\lib\site-packages\flask_sqlalchemy\", line 943, in engine
    return self.get_engine()
  File "c:\coding-projects\task-master-tdd\env\lib\site-packages\flask_sqlalchemy\", line 962, in get_engine
    return connector.get_engine()
  File "c:\coding-projects\task-master-tdd\env\lib\site-packages\flask_sqlalchemy\", line 555, in get_engine
    options = self.get_options(sa_url, echo)
  File "c:\coding-projects\task-master-tdd\env\lib\site-packages\flask_sqlalchemy\", line 570, in get_options
    self._sa.apply_driver_hacks(self._app, sa_url, options)
  File "c:\coding-projects\task-master-tdd\env\lib\site-packages\flask_sqlalchemy\", line 914, in apply_driver_hacks
    sa_url.database = os.path.join(app.root_path, sa_url.database)
AttributeError: can't set attribute

How To Solve the Error “AttributeError: can’t set attribute when connecting to sqlite database with flask-sqlalchemy”

We have a few solutions that can help you resolve the error efficiently

Solution 1 – Downgrade the version of SQLAlchemy

The simplest solution to sort out the error is to downgrade the SQLAlchemy version. You need to follow the below steps to successfully downgrade the version

  • Use the pip freeze command to be double sure that the issue is affecting or not
  • You get to know that the SQLAlchemy current version is 1.4.0.
  • The best way is to manually downgrade the version using the below command
pip install SQLAlchemy==1.3.23

This is how you can simply resolve the error.

Solution 2 – Pinning SQLAlchemy

Another way to fix the error is to pin the SQLAlchemy lower version than 1.4.0. To do that, follow the below steps

To freeze the package

pip freeze > requirements.txt

In the requirements.txt, upgrade SQLAlchemy


In order to reinstall packages, use the below command

pip install -r requirements.txt

Solution 3 – Check if the version is updated automatically

Sometimes the auto updates cause the error. SQLALchemy works as a dependency by auto-installing for Flask-SQLAlchemy. The latest version (1.4.0) brought the below 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.

To fix the error, you just need to install the version you previously had.


We shed light on the solutions to help you fix the error “AttributeError: can’t set attribute when connecting to sqlite database with flask-sqlalchemy”. I hope you find it helpful!

Leave a Reply

Your email address will not be published. Required fields are marked *