RuntimeError: Either 'SQLALCHEMY_DATABASE_URI' or 'SQLALCHEMY_BINDS' must be set

avatar
Borislav Hadzhiev

Last updated: Apr 13, 2024
3 min

banner

# RuntimeError: Either 'SQLALCHEMY_DATABASE_URI' or 'SQLALCHEMY_BINDS' must be set

The Python "RuntimeError: Either 'SQLALCHEMY_DATABASE_URI' or 'SQLALCHEMY_BINDS' must be set" occurs when you don't initialize your database correctly or set the database URI to an incorrect value.

To solve the error, make sure to initialize your database correctly and set the database URI to a relative or absolute path that points to your .db file.

runtime error either sqlalchemy database uri or sqlalchemy binds must be set

Here is a minimal example of correctly initializing your database in a Flask application and setting the SQLALCHEMY_DATABASE_URI configuration option.

app.py
from flask import Flask,render_template from flask_sqlalchemy import SQLAlchemy # 1) Initialize your DATABASE db = SQLAlchemy() # 2) Create your Flask Application app = Flask(__name__) # 3) Set the DATABASE URI app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///example.db' # 4) Initialize a Flask App to use for the DATABASE db.init_app(app) @app.route('/') def index(): return render_template('index.html') if __name__ == '__main__': app.run(debug = True)

If I start my development server with python app.py, I can see that the error has been resolved.

shell
python app.py

dev server started successfully

Here are the steps.

app.py
from flask import Flask,render_template from flask_sqlalchemy import SQLAlchemy # 1) Initialize your DATABASE db = SQLAlchemy() # 2) Create your Flask Application app = Flask(__name__) # 3) Set the DATABASE URI app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///example.db' # 4) Initialize a Flask App to use for the DATABASE db.init_app(app)

Make sure to follow the same order of operations.

The app.config... line must run before db.init_app(app).

You will get the error if you set the SQLALCHEMY_DATABASE_URI option after having initialized the Flask App to use for the database.

We first, initialize the database and then create the Flask application.

The next line sets the SQLALCHEMY_DATABASE_URI configuration option.

# Make sure to use three forward slashes in the connection URI

Notice that we used three slashes in the URI.

app.py
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///example.db'

If you only use 2 slashes you'd get the "sqlalchemy.exc.ArgumentError: Invalid SQLite URL: sqlite://example.db " error.

Make sure to use 3 forward slashes in your connection URL as in sqlite:///example.db.

# Make sure you haven't misspelled SQLALCHEMY_DATABASE_URI

Another thing to look out for is that you don't misspell SQLALCHEMY_DATABASE_URI.

Misspelling the configuration option is equivalent to not setting it at all.

You can copy-paste the following line and update the connection URI as needed.

app.py
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///example.db'

Notice that the setting is SQLALCHEMY_DATABASE_URI and not SQLALCHEMY_DATABASE_URL.

In other words, it is _URI and not _URL.

# Specifying your Database Connection URI as an absolute path

If the error persists, you can try to specify your database URI as an absolute path.

For example, the absolute path to my .db file is: /home/borislav/Desktop/bobbyhadz-rest/bobbyhadz-flask/example.db.

So, I would use the following code sample.

app.py
from flask import Flask,render_template from flask_sqlalchemy import SQLAlchemy # 1) Initialize your DATABASE db = SQLAlchemy() # 2) Create your Flask Application app = Flask(__name__) # 3) Set the DATABASE URI app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////home/borislav/Desktop/bobbyhadz-rest/bobbyhadz-flask/example.db' # 4) Initialize a Flask App to use for the DATABASE db.init_app(app) @app.route('/') def index(): return render_template('index.html') if __name__ == '__main__': app.run(debug = True)

specifying database uri as absolute path

Notice that I now have 4 forward slashes (instead of 3) after sqlite: because my absolute path starts with a forward slash.

app.py
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////home/borislav/Desktop/bobbyhadz-rest/bobbyhadz-flask/example.db'

# You can also use the following initialization code

You might also see examples that use the following initialization code which works perfectly fine.

app.py
from flask import Flask,render_template from flask_sqlalchemy import SQLAlchemy # 1) Create your Flask Application app = Flask(__name__) # 2) Set the DATABASE URI app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///example.db' # 3) Initialize a Flask App to use for the DATABASE db = SQLAlchemy(app) @app.route('/') def index(): return render_template('index.html') if __name__ == '__main__': app.run(debug = True)

alternative initialization code

The code sample:

  1. Creates the Flask application.
app.py
# 1) Create your Flask Application app = Flask(__name__)
  1. Sets the SQLALCHEMY_DATABASE_URI configuration option.
app.py
# 2) Set the DATABASE URI app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///example.db'
  1. Initializes the Flask App to use for the database.
main.py
# 3) Initialize a Flask App to use for the DATABASE db = SQLAlchemy(app)

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.