0

table is found but still getting the table not found error where my main file is main.py

import uvicorn
from starlette.applications import Starlette
from starlette.routing import Route
from API.get import GetMethod
from API.post import PostMethod
from db import engine

from sqlalchemy.orm import declarative_base
Base = declarative_base()
routes = [Route("/get", endpoint=GetMethod, methods=["GET"]),
          Route("/post", endpoint=PostMethod, methods=["POST"]),
          ]
app = Starlette(
    routes=routes
)
@app.on_event("startup")
def on_startup():
    Base.metadata.create_all(engine)
if __name__ == "__main__":
    uvicorn.run("carsharing:app", reload=True)

Database table file and schemas.py:

from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class PersonDetail(Base):
    __tablename__ = 'PersonDetail'
    id = Column(Integer, primary_key=True)
    name = Column(String)

get.py is

from starlette.requests import Request
from starlette.responses import PlainTextResponse, JSONResponse, RedirectResponse, Response
from db import engine
from schemas import PersonDetail
from sqlalchemy.orm import Session
local_session = Session(bind=engine, expire_on_commit=False)
def GetMethod(request):
    return local_session.query(PersonDetail).all()

Database file is db.py is

from sqlalchemy.orm import Session
from sqlalchemy import create_engine

engine = create_engine(
    "sqlite:///PersonalDetail.db",
    connect_args={"check_same_thread": False},
    echo=True)


def get_session():
    with Session(engine) as session:
        yield session
MatsLindh
  • 49,529
  • 4
  • 53
  • 84
SPMK
  • 19
  • 5
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jul 06 '22 at 19:24
  • You're creating multiple instances of `Base` - but only you're only running `create_all` for one of them. Make sure to use the same `Base` for all your models and your `create_all` call. I'm also not sure where you import your models (your schemas.py - usually schemas describe the input/output mappings in FastAPI and not your database models) before calling `create_all`. – MatsLindh Jul 06 '22 at 19:24
  • Getting an operational error as there is no table created. How to resolve this – SPMK Jul 06 '22 at 19:45

0 Answers0