RuntimeError: Either 'SQLALCHEMY_DATABASE_URI' or 'SQLALCHEMY_BINDS' must be set.
时间: 2024-01-16 07:05:31 浏览: 539
This error message indicates that the Flask-SQLAlchemy extension was not able to find the configuration variable `SQLALCHEMY_DATABASE_URI` or `SQLALCHEMY_BINDS` in your Flask application's configuration.
To fix this error, you need to make sure that you have set the `SQLALCHEMY_DATABASE_URI` or `SQLALCHEMY_BINDS` configuration variable in your Flask application's configuration.
Here is an example of how to set the `SQLALCHEMY_DATABASE_URI` configuration variable:
```
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://username:password@localhost/mydatabase'
```
In this example, `username` and `password` should be replaced with your PostgreSQL database username and password, `localhost` should be replaced with the hostname of your PostgreSQL server, and `mydatabase` should be replaced with the name of your PostgreSQL database.
If you need to connect to multiple databases, you can set the `SQLALCHEMY_BINDS` configuration variable instead:
```
app.config['SQLALCHEMY_BINDS'] = {
'db1': 'postgresql://username1:password1@localhost/db1',
'db2': 'postgresql://username2:password2@localhost/db2'
}
```
In this example, `db1` and `db2` are the names of the databases that you want to connect to, and `username1`, `password1`, `username2`, and `password2` should be replaced with the appropriate credentials for each database.
阅读全文