python-gino/gino-starlette

gino.exceptions.UninitializedError: Gino engine is not initialized.

begyy opened this issue · 11 comments

begyy commented

Steps to Reproduce the Problem

  1. gino.exceptions.UninitializedError: Gino engine is not initialized.
    image

  2. view
    image
    3 settings
    image

  3. model
    image

Specifications

  • Python version: 3.6.5
  • GINO version:0.8.5
  • Starlette version:0.13.1

Thanks for trying this extension in such an early stage! We @YUMEYA are still working on this, please kindly wait for the initial release, or use GINO 0.8 for now.

begyy commented

Okey)

@begyy Oh sorry I didn't notice the issue is for GINO 0.8.5. I'll leave that to @YUMEYA to solve.

Hello @begyy , I can't reproduce in the same conditions. Please double check your postgresql settings, especially the privileges of your user on the database.

begyy commented

Hello @YUMEYA I checked the database. on this exemplar https://github.com/python-gino/gino-starlette/blob/master/example/app.py works does not work on mine:)) Can you show me another example? with a router

@begyy OK, I'll look into it.

@begyy I changed your code as following and worked:

  1. main.py
import os

import uvicorn
from gino.ext.starlette import Gino
from starlette.applications import Starlette

DB_ARGS = dict(
    host=os.getenv("DB_HOST", "localhost"),
    port=os.getenv("DB_PORT", 5432),
    user=os.getenv("DB_USER", "postgres"),
    password=os.getenv("DB_PASS", ""),
    database=os.getenv("DB_NAME", "gino"),
)
PG_URL = "postgresql://{user}:{password}@{host}:{port}/{database}".format(**DB_ARGS)

# Initialize Starlette app
# Don't pass `route` param here, register route with `app.route()` decorator later in `view.py`.
app = Starlette()

# Initialize Gino object
db = Gino(dsn=PG_URL)
db.init_app(app)

# Import your route functions to make them effective. 
# To prevent cyclic importing (`view.py` and `user.py`
# will import `app` and `db` instances above),
# don't put it at the top of this file. 
from view import *

if __name__ == "__main__":
    uvicorn.run(app, host="127.0.0.1", port=5000)
  1. view.py
from starlette.responses import JSONResponse
from user import User

# Import starlette app instance from `main.py`.
from main import app

# Register your route.
@app.route("/")
async def homepage(request):
    results = []
    users = await User.query.gino.all()
    for user in users:
        results.append({"id": user.id, "name": user.username})
    return JSONResponse(results)
  1. user.py
# Import gino instance from main.py
from main import db


class User(db.Model):
    __tablename__ = "gino_users"

    id = db.Column(db.BigInteger(), primary_key=True)
    username = db.Column(db.String(50), default="unnamed")
    password = db.Column(db.String(250))

Here's my screenshot:
image

begyy commented

@YUMEYA thanks a lot

@begyy You are welcome! Feel free to ask any questions or suggest any ideas on our project! 😊

begyy commented

@YUMEYA okey 😄