You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here's the important detail, and probably the most important feature of **SQLModel**: only `Hero` is declared with `table = True`.
119
121
120
122
This means that the class `Hero` represents a **table** in the database. It is both a **Pydantic** model and a **SQLAlchemy** model.
@@ -163,16 +165,24 @@ In versions of **SQLModel** before `0.0.14` you would use the method `.from_orm(
163
165
164
166
We can now create a new `Hero` instance (the one for the database) and put it in the variable `db_hero` from the data in the `hero` variable that is the `HeroCreate` instance we received from the request.
Then we just `add` it to the **session**, `commit`, and `refresh` it, and finally, we return the same `db_hero` variable that has the just refreshed `Hero` instance.
169
175
170
176
Because it is just refreshed, it has the `id` field set with a new ID taken from the database.
171
177
172
178
And now that we return it, FastAPI will validate the data with the `response_model`, which is a `HeroPublic`:
@@ -304,13 +324,18 @@ On top of that, we could easily decide in the future that we want to receive **m
304
324
Now let's check the `HeroPublic` model.
305
325
306
326
This one just declares that the `id` field is required when reading a hero from the API, because a hero read from the API will come from the database, and in the database it will always have an ID.
The FastAPI code is still the same as above, we still use `Hero`, `HeroCreate`, and `HeroPublic`. But now, we define them in a smarter way with inheritance.
0 commit comments