Gino engine is not initialized

Viewed 1491

I'm trying to create a simple Restful API using aiohttp and Gino.

Inside my endpoint class, I got the following:

class ListEndpoint(Endpoint):
def __init__(self, model):
    super().__init__()
    self.model = model

async def get(self) -> Response:
    async with db.with_bind(DB_ADDRESS):
        obj_list = await self.model.query.gino.all()

    if not obj_list:
        return Response(
            status=404,
            body=json.dumps({'Not found': 404}),
            content_type='application/json'
        )

    data = await ModelSerializer(obj_list).to_json()

    return Response(
        status=200, body=data, content_type='application/json'
    )

This is the only place where I call gino. This is my main.py:

from aiohttp.web import Application, run_app

from models import Country
from resource import GenericResource


app = Application()
countries = GenericResource('countries', Country)
countries.register(app.router)


if __name__ == '__main__':
    run_app(app)

When I call the route with a GET request, I get the following error:

Gino engine is not initialized.

And it fails on this:

async with db.with_bind(DB_ADDRESS):
    obj_list = await self.model.query.gino.all()

Is it a bad place to bind? Or, as I can think of the reasons of this issue, is that this code goes to the event loop and doesn't have time to bind when obj_list is set.

1 Answers

The problem was I created different instances of Gino class in different files. This is wrong. As a solution, you should create only 1 instance of Gino and share it between different files.

Related