python-gino / gino-starlette

An extension for GINO to support Starlette server.
https://python-gino.org
Other
78 stars 24 forks source link

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

Closed begyy closed 4 years ago

begyy commented 4 years ago

Steps to Reproduce the Problem

  1. gino.exceptions.UninitializedError: Gino engine is not initialized. image
  1. view image 3 settings image
  2. model image

Specifications

fantix commented 4 years ago

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 4 years ago

Okey)

fantix commented 4 years ago

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

YUMEYA commented 4 years ago

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 4 years ago

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 commented 4 years ago

@YUMEYA can you check my code? https://github.com/begyy/gino_and_starlette_hello_world ?)

YUMEYA commented 4 years ago

@begyy OK, I'll look into it.

YUMEYA commented 4 years ago

@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 4 years ago

@YUMEYA thanks a lot

YUMEYA commented 4 years ago

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

begyy commented 4 years ago

@YUMEYA okey 😄