Last updated: Apr 13, 2024
Reading time·3 min
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.
Here is a minimal example of correctly initializing your database in a Flask
application and setting the SQLALCHEMY_DATABASE_URI
configuration option.
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.
python app.py
Here are the steps.
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)
.
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.
Notice that we used three slashes in the URI.
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
.
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.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
.
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.
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)
Notice that I now have 4 forward slashes (instead of 3) after sqlite:
because
my absolute path starts with a forward slash.
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////home/borislav/Desktop/bobbyhadz-rest/bobbyhadz-flask/example.db'
You might also see examples that use the following initialization code which works perfectly fine.
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)
The code sample:
# 1) Create your Flask Application app = Flask(__name__)
SQLALCHEMY_DATABASE_URI
configuration option.# 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)
You can learn more about the related topics by checking out the following tutorials: