gino.exceptions.UninitializedError: Gino engine is not initialized.
begyy opened this issue · 11 comments
begyy commented
fantix commented
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)
fantix commented
YUMEYA commented
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 commented
@YUMEYA can you check my code? https://github.com/begyy/gino_and_starlette_hello_world ?)
YUMEYA commented
@begyy I changed your code as following and worked:
- 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)
- 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)
- 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))