-
-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Getting AssertionError: DatabaseBackend is not running
for Postgres
#180
Comments
For all backends except sqlite you need to connect the database to use it. You use fastapi so you should follow example in fastapi section of the ormar documentation. Basically you need to add connect and disconnect to fastapi instance events. @app.on_event("startup")
async def startup() -> None:
database_ = app.state.database
if not database_.is_connected:
await database_.connect()
@app.on_event("shutdown")
async def shutdown() -> None:
database_ = app.state.database
if database_.is_connected:
await database_.disconnect() As described in https://github.com/collerek/ormar/blob/master/docs/fastapi.md#database-connection |
So it's not a bug. Note that is has to be the same database instance you use in models so you need to import it to main. |
Thanks a lot for the quick reply. Yes definetely not a bug, it was kind of unexpected that without that the sqlite README code was working and the Postgres not. Maybe it is worth mentioning in the main page that for other DBs some initialisation steps are necessary, check framework for examples. |
Good idea, you can also connect for sqlite it does not hurt so maybe I should add that to quick start. |
I think it is actually great idea to add this code to the quickstart, as one would be good to go, no matter the DB. Having code that works out of the box for things is what makes people love frameworks (talking at least for me) Thanks for being so committed to the community :) |
Thanks a lot ! |
Describe the bug
I used the sample code from README to connect to a sqlite DB and query items and it worked fine (See https://github.com/mei-li/tsougkrizo/blob/persistency/persistence.py). When I switched to a Postgres DB the schema initialisation worked, but when I query the items I get:
To Reproduce
Steps to reproduce the behavior:
/demo_ormar_issue
in main.py matter)export DATABASE_URL=postgresql://dev:test@localhost/egg_db
python create_db.py
# this works and creates the DB schema(Note: this should be a complete and concise piece of code that allows reproduction of an issue)
Expected behavior
I expect the query to objects in the DB to work without this error
Versions (please complete the following information):
ormar
version 0.10.4pydantic
version 1.8fastapi
version 0.54.1The text was updated successfully, but these errors were encountered: