sqlmodel+fastapi+uvicorn: 3 of 4 startoptions resulting in "sqlalchemy.exc.InvalidRequestError: Table 'team' is already defined for this MetaData instance." #953
-
First Check
Commit to Help
Example Code#!env python3
## Starting with one of these methods:
## a) ./bla.py
## b) python3 bla.py
## c) uvicorn "bla:app" --reload --host localhost --port 8000
## d) Run or Debugger in vscode
import uvicorn
from fastapi import FastAPI
from fastapi import Cookie, Header, Form, status, Path, Query, Body, Response
from sqlmodel import Field, Relationship, SQLModel
class TeamBase(SQLModel):
name: str = Field(index=True)
headquarters: str
## c) is running
## a, b and d are crashing with:
## sqlalchemy.exc.InvalidRequestError: Table 'team' is already defined for this MetaData instance. \
## Specify 'extend_existing=True' to redefine options and columns on an existing Table object.
class Team(TeamBase, table=True):
id: int | None = Field(default=None, primary_key=True)
app = FastAPI()
@app.get("/")
def root():
return {"hello worldd": 42}
if __name__ == "__main__":
uvicorn.run("bla:app", host="0.0.0.0", port=8000, reload=True) DescriptionOnly startoption c) (uvicron from shell) do not produce the errror. I used an example from SQL-Model online-documentation Operating SystemLinux, macOS Operating System DetailsNo response SQLModel Version0.0.18 Python VersionPython 3.12.3 Additional ContextWorking with vscode and internal debugger. The same problem. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Sorry, I'm a new in programming python and nearly zero python skills. Code: import logging
import sys
# Create a custom logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG) # Set the default log level
# Create handlers
stdout_handler = logging.StreamHandler(sys.stdout)
stderr_handler = logging.StreamHandler(sys.stderr)
# Set levels for handlers
stdout_handler.setLevel(logging.DEBUG)
stderr_handler.setLevel(logging.ERROR)
# Create formatters and add them to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
stdout_handler.setFormatter(formatter)
stderr_handler.setFormatter(formatter)
# Add handlers to the logger
logger.addHandler(stdout_handler)
logger.addHandler(stderr_handler)
# Example usage
logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.critical('This is a critical message') Log ./bla.py
Log: uvicorn "bla:app" --reload --host localhost --port 8000
|
Beta Was this translation helpful? Give feedback.
-
You should move models to separate module and import it to your main module models.py from fastapi import Cookie, Header, Form, status, Path, Query, Body, Response
from sqlmodel import Field, Relationship, SQLModel
class TeamBase(SQLModel):
name: str = Field(index=True)
headquarters: str
class Team(TeamBase, table=True):
id: int | None = Field(default=None, primary_key=True) bla.py import uvicorn
from fastapi import FastAPI
from models import Team
app = FastAPI()
@app.get("/")
def root():
return {"hello worldd": 42}
if __name__ == "__main__":
uvicorn.run("bla:app", host="0.0.0.0", port=8000, reload=True) |
Beta Was this translation helpful? Give feedback.
-
Thanks for the answer, i will try it out |
Beta Was this translation helpful? Give feedback.
You should move models to separate module and import it to your main module
models.py
bla.py