Using `check_same_thread=False` allows FastAPI to use the same SQLite database in different threads. This is necessary as **one single request** could use **more than one thread** (for example in dependencies).
@ -87,7 +87,7 @@ Don't worry, with the way the code is structured, we'll make sure we use **a sin
We then add a function that uses `SQLModel.metadata.create_all(engine)` to **create the tables** for all the *table models*.
Here we create the tables on an application startup event.
For production you would probably use a migration script that runs before you start your app. 🤓
/// warning
The `@app.on_event("startup")` and `@app.on_event("shutdown")` decorators are **deprecated** as of FastAPI v0.103.0.
Use the <ahref="https://fastapi.tiangolo.com/advanced/events/#lifespan"class="external-link"target="_blank">`lifespan`</a> parameter in the `FastAPI` class instead for lifecycle management.
///
/// tip
SQLModel will have migration utilities wrapping Alembic, but for now, you can use <ahref="https://alembic.sqlalchemy.org/en/latest/"class="external-link"target="_blank">Alembic</a> directly.
@ -123,7 +133,7 @@ For example, if you declare a parameter of type `Hero`, it will be read from the
The same way, you can declare it as the function's **return type**, and then the shape of the data will show up in the automatic API docs UI.
Here we use the `SessionDep` dependency (a `Session`) to add the new `Hero` to the `Session` instance, commit the changes to the database, refresh the data in the `hero`, and then return it.
@ -131,19 +141,19 @@ Here we use the `SessionDep` dependency (a `Session`) to add the new `Hero` to t
We can **read**`Hero`s from the database using a `select()`. We can include a `limit` and `offset` to paginate the results.
### Create with `HeroCreate` and return a `HeroPublic`
@ -288,7 +298,7 @@ This new *table model* `Hero` will have the fields sent by the client, and will
Then we return the same *table model*`Hero` as is from the function. But as we declare the `response_model` with the `HeroPublic`*data model*, **FastAPI** will use `HeroPublic` to validate and serialize the data.
@ -304,13 +314,13 @@ By declaring it in `response_model` we are telling **FastAPI** to do its thing,
We can do the same as before to **read**`Hero`s, again, we use `response_model=list[HeroPublic]` to ensure that the data is validated and serialized correctly.