-
-
Notifications
You must be signed in to change notification settings - Fork 785
Closed
Labels
questionFurther information is requestedFurther information is requested
Description
First Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn't find it.
- I searched the SQLModel documentation, with the integrated search.
- I already searched in Google "How to X in SQLModel" and didn't find any information.
- I already read and followed all the tutorial in the docs and didn't find an answer.
- I already checked if it is not related to SQLModel but to Pydantic.
- I already checked if it is not related to SQLModel but to SQLAlchemy.
Commit to Help
- I commit to help with one of those options 👆
Example Code
from typing import List, Optional
from fastapi import Depends, FastAPI, HTTPException, Query
from sqlmodel import Field, Session, SQLModel, create_engine, select
class TeamBase(SQLModel):
name: str
headquarters: str
class Team(TeamBase, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
class TeamCreate(TeamBase):
pass
class TeamRead(TeamBase):
id: int
db_url = "mysql://user:pass@localhost/db"
engine = create_engine(db_url)
def create_db_and_tables():
SQLModel.metadata.create_all(engine)
def get_session():
with Session(engine) as session:
yield session
app = FastAPI()
@app.on_event("startup")
def on_startup():
create_db_and_tables()
@app.post("/teams/", response_model=TeamRead)
def create_team(*, session: Session = Depends(get_session), team: TeamCreate):
db_team = Team.from_orm(team)
session.add(db_team)
session.commit()
session.refresh(db_team)
return db_team
@app.get("/teams/", response_model=List[TeamRead])
def read_teams(
*,
session: Session = Depends(get_session),
offset: int = 0,
limit: int = Query(default=100, lte=100),
):
teams = session.exec(select(Team).offset(offset).limit(limit)).all()
return teamsDescription
After an period of inactivity (have not yet isolated how long, roughly a few mins however) the following error will come up.
sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (2013, 'Lost connection to MySQL server during query')
It appears to overcome this in SQLAlchemy, during engine creation you add pool_recycle. eg
engine = create_engine("mysql://user:pass@localhost/db", pool_recycle=1800)However if you do the same in SQLmodel the following error occurs.
ERROR: Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 540, in lifespan
async for item in self.lifespan_context(app):
File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 481, in default_lifespan
await self.startup()
File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 518, in startup
handler()
File "/home/brentdreyer/Documents/automation/mnf_database_application/./app/main.py", line 24, in on_startup
create_db_and_tables()
File "/home/brentdreyer/Documents/automation/mnf_database_application/./app/db/session.py", line 8, in create_db_and_tables
SQLModel.metadata.create_all(engine, pool_recycle=1800)
TypeError: create_all() got an unexpected keyword argument 'pool_recycle'
Operating System
Linux
Operating System Details
Fedora KDE 34
SQLModel Version
0.0.4
Python Version
Python 3.9.6
Additional Context
No response
Metadata
Metadata
Assignees
Labels
questionFurther information is requestedFurther information is requested