python-gino / gino

GINO Is Not ORM - a Python asyncio ORM on SQLAlchemy core.
https://python-gino.org/
Other
2.68k stars 150 forks source link

Gino Object doesn't expose Bakery #701

Closed Chaostheorie closed 4 years ago

Chaostheorie commented 4 years ago

Description

The bake attribute/ the bakery is missing from the gino.ext.starlette.Gino object. This raises an error if an query should be baked with `db.bake (db = Gino(app, dsn=Config.DATABASE_URL) and db is connected). Schema declaration and normal queries/ updates/ create are fully functional.

What I Did

This block is executed after the init of app and db on startup.

async def connect():
    from app import app, db
    from app.models import (Game, PlayerGames, Player, Role,
                            PlayerRoles, State, GameStates)
    app.engine = await db.set_bind(Config.DATABASE_URL)
    app.baked = Ctx()
    await db.gino.create_all()
    app.logger.info("Database created and connected")
   # At this point 'bake' is also not in dir(db)
async def QueryBake():
    from app import app, db
    from app.models import Player, PlayerGames, Role, PlayerRoles, Game

    # User.get_roles() query
    query = Role.outerjoin(PlayerRoles).outerjoin(Player)
    query = query.select().where(
        PlayerRoles.player_id == db.bindparam("uid")
        )
    loader = Role.distinct(Role.id).load(
        add_parent=Player.distinct(Player.id)
        )

    app.baked.role_query = db.bake(
        query.execution_options(loader=loader)
    )

Traceback:

´bash ERROR: Traceback (most recent call last): File "/home/username/P/boardgame-backend/venv/lib/python3.8/site-packages/starlette/routing.py", line 517, in lifespan await self.startup() File "/home/username/P/boardgame-backend/venv/lib/python3.8/site-packages/starlette/routing.py", line 494, in startup await handler() File "/home/username/P/boardgame-backend/app/config.py", line 73, in QueryBake app.baked.role_query = db.bake( AttributeError: 'Gino' object has no attribute 'bake' ´

Pentusha commented 4 years ago

AFAIK, The bakery is not yet published to PyPI.

wwwjfy commented 4 years ago

It's included in 1.0.1. @Chaostheorie can you upgrade and try again?

@fantix let's include that in change log too :)

Chaostheorie commented 4 years ago

The problem consists after upgrading. (Note: The gino.__version__=1.0.1 shows the upgrade as well as pip) same traceback. Is this maybe an error with gino-starlette and the Bakery?

Chaostheorie commented 4 years ago

@Pentusha Is the 1.0.1 too?

fantix commented 4 years ago

Oh, I'm sorry, baked query is not backported to 1.0.x, it's new in 1.1.

I'll add a more significant development note to the documentation of the master version.

wwwjfy commented 4 years ago

@Chaostheorie yeah sorry for misleading. I didn't check the commits carefully.

Chaostheorie commented 4 years ago

@fantix Are there any significant other changes bound to Gino.bake? Otherwise could a workaround such as overriding the original Gino Object and just adding the bake method work?

Chaostheorie commented 4 years ago

Or is there a good way to use the newer version with pip by referencing the git URL?

fantix commented 4 years ago

I was hoping to release 1.1-beta1 with MySQL support, but I guess we could release 1.1-beta1 without MySQL first, and include it in beta2.

Chaostheorie commented 4 years ago

I close the issue since the original problem was caused by me not reading the master branche warning.

@fantix Can you give me an estimated amount of time I need to wait for the 1.1-beta1 on pypi

fantix commented 4 years ago

caused by me not reading the master branche warning

Sorry that was on me - the warning is just added after this issue.

an estimated amount of time I need to wait for the 1.1-beta1 on pypi

It's now available here: https://pypi.org/project/gino/1.1.0b1/

Chaostheorie commented 4 years ago

Thanks a lot.

Chaostheorie commented 4 years ago

Have some issues with the bind after using baked queries. Seems like the Bakery when creating the bind on initializing the Gino object. This happens when using await db.set_bind(URL) (db=initialized Gino object) without even interacting any further. Using the prebake kwrag doesn't change this behavoiur

Version: GINO 1.1.0-beta.1

Traceback:
  File "/home/chaostheorie/P/boardgame-backend/venv/lib/python3.8/site-packages/starlette/routing.py", line 517, in lifespan
    await self.startup()
  File "/home/chaostheorie/P/boardgame-backend/venv/lib/python3.8/site-packages/starlette/routing.py", line 494, in startup
    await handler()
  File "/home/chaostheorie/P/boardgame-backend/venv/lib/python3.8/site-packages/gino_starlette.py", line 177, in startup
    await self.set_bind(
  File "/home/chaostheorie/P/boardgame-backend/venv/lib/python3.8/site-packages/gino_starlette.py", line 226, in set_bind
    return await super().set_bind(bind, loop=loop, **kwargs)
  File "/home/chaostheorie/P/boardgame-backend/venv/lib/python3.8/site-packages/gino/api.py", line 427, in set_bind
    bind = await create_engine(bind, loop=loop, bakery=self._bakery, **kwargs)
  File "/home/chaostheorie/P/boardgame-backend/venv/lib/python3.8/site-packages/gino/strategies.py", line 53, in create
    dialect = dialect_cls(**dialect_args)
  File "/home/chaostheorie/P/boardgame-backend/venv/lib/python3.8/site-packages/gino/dialects/asyncpg.py", line 513, in __init__
    self._init_mixin(bakery)
  File "/home/chaostheorie/P/boardgame-backend/venv/lib/python3.8/site-packages/gino/dialects/base.py", line 420, in _init_mixin
    raise InitializedError("Cannot reuse a closed bakery!")
gino.exceptions.InitializedError: Cannot reuse a closed bakery!
from app import app, db
from app.models import (Game, PlayerGames, Player, Role,  PlayerRoles, State, GameStates)
await db.set_bind(Config.DATABASE_URL, prebake=False)
await db.gino.create_all()
app.logger.info("Database created and connected")