Skip to content
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

Closed
mei-li opened this issue Apr 24, 2021 · 6 comments
Closed

Getting AssertionError: DatabaseBackend is not running for Postgres #180

mei-li opened this issue Apr 24, 2021 · 6 comments
Labels
bug Something isn't working

Comments

@mei-li
Copy link

mei-li commented Apr 24, 2021

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:

  File "./main.py", line 139, in websocket_host
    game = await game_manager.get_game(game_id)
  File "./main.py", line 82, in get_game
    game = await Game.objects.get(uuid=game_id)
  File "/home/olkos/.pyenv/versions/3.8.2/envs/tsougkrizo/lib/python3.8/site-packages/ormar/queryset/queryset.py", line 829, in get
    return await self.filter(*args, **kwargs).get()
  File "/home/olkos/.pyenv/versions/3.8.2/envs/tsougkrizo/lib/python3.8/site-packages/ormar/queryset/queryset.py", line 845, in get
    rows = await self.database.fetch_all(expr)
  File "/home/olkos/.pyenv/versions/3.8.2/envs/tsougkrizo/lib/python3.8/site-packages/databases/core.py", line 139, in fetch_all
    async with self.connection() as connection:
  File "/home/olkos/.pyenv/versions/3.8.2/envs/tsougkrizo/lib/python3.8/site-packages/databases/core.py", line 219, in __aenter__
    await self._connection.acquire()
  File "/home/olkos/.pyenv/versions/3.8.2/envs/tsougkrizo/lib/python3.8/site-packages/databases/backends/postgres.py", line 148, in acquire
    assert self._database._pool is not None, "DatabaseBackend is not running"
AssertionError: DatabaseBackend is not running

To Reproduce
Steps to reproduce the behavior:

  1. Go to https://github.com/mei-li/tsougkrizo/tree/persistency (only persistence.py and the sample url /demo_ormar_issue in main.py matter)
  2. Create and install environment of the project with python 3.8
  3. Export an existing postgres DB url eg. export DATABASE_URL=postgresql://dev:test@localhost/egg_db
  4. Run python create_db.py # this works and creates the DB schema
  5. Run uvicorn main:app --reload
  6. Click on http://127.0.0.1:8000/demo_ormar_issue
  7. See error

(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):

  • Database backend used (mysql/sqlite/postgress)
  • Python version 3.8.2
  • ormar version 0.10.4
  • pydantic version 1.8
  • if applicable fastapi version 0.54.1
@mei-li mei-li added the bug Something isn't working label Apr 24, 2021
@collerek
Copy link
Owner

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

@collerek
Copy link
Owner

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.

@mei-li
Copy link
Author

mei-li commented Apr 24, 2021

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.

@collerek
Copy link
Owner

Good idea, you can also connect for sqlite it does not hurt so maybe I should add that to quick start.

@mei-li
Copy link
Author

mei-li commented Apr 28, 2021

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 :)

@ppostnov
Copy link

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

Thanks a lot !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants