📝 Update docs (#1003)
This commit is contained in:
parent
b8d7f4ff67
commit
da1253c21f
@ -4,7 +4,7 @@ In the previous chapter, we saw how to add rows to the database using **SQLModel
|
|||||||
|
|
||||||
Now let's talk a bit about why the `id` field **can't be `NULL`** on the database because it's a **primary key**, and we declare it using `Field(primary_key=True)`.
|
Now let's talk a bit about why the `id` field **can't be `NULL`** on the database because it's a **primary key**, and we declare it using `Field(primary_key=True)`.
|
||||||
|
|
||||||
But the same `id` field actually **can be `None`** in the Python code, so we declare the type with `Optional[int]`, and set the default value to `Field(default=None)`:
|
But the same `id` field actually **can be `None`** in the Python code, so we declare the type with `int | None (or Optional[int])`, and set the default value to `Field(default=None)`:
|
||||||
|
|
||||||
//// tab | Python 3.10+
|
//// tab | Python 3.10+
|
||||||
|
|
||||||
|
@ -145,7 +145,7 @@ Let's now see with more detail these field/column declarations.
|
|||||||
|
|
||||||
### Optional Fields, Nullable Columns
|
### Optional Fields, Nullable Columns
|
||||||
|
|
||||||
Let's start with `age`, notice that it has a type of `Optional[int]`.
|
Let's start with `age`, notice that it has a type of `int | None (or Optional[int])`.
|
||||||
|
|
||||||
And we import that `Optional` from the `typing` standard module.
|
And we import that `Optional` from the `typing` standard module.
|
||||||
|
|
||||||
|
@ -134,7 +134,7 @@ Now let's do all that, but this time using the new, shiny `Relationship` attribu
|
|||||||
|
|
||||||
Now we can create the `Team` instances and pass them directly to the new `team` argument when creating the `Hero` instances, as `team=team_preventers` instead of `team_id=team_preventers.id`.
|
Now we can create the `Team` instances and pass them directly to the new `team` argument when creating the `Hero` instances, as `team=team_preventers` instead of `team_id=team_preventers.id`.
|
||||||
|
|
||||||
And thanks to SQLAlchemy and how it works underneath, these teams don't even have to have an ID yet, but because we are assigning the whole object to each hero, those teams **will be automatically created** in the database, the automatic ID will be generated, and will be set in the `team_id` column for each of the corresponding hero rows.
|
And thanks to SQLAlchemy and how it works underneath, these teams don't even need to have an ID yet, but because we are assigning the whole object to each hero, those teams **will be automatically created** in the database, the automatic ID will be generated, and will be set in the `team_id` column for each of the corresponding hero rows.
|
||||||
|
|
||||||
In fact, now we don't even have to put the teams explicitly in the session with `session.add(team)`, because these `Team` instances are **already associated** with heroes that **we do** `add` to the session.
|
In fact, now we don't even have to put the teams explicitly in the session with `session.add(team)`, because these `Team` instances are **already associated** with heroes that **we do** `add` to the session.
|
||||||
|
|
||||||
|
@ -1250,7 +1250,7 @@ It would be an error telling you that
|
|||||||
|
|
||||||
> `Hero.age` is potentially `None`, and you cannot compare `None` with `>`
|
> `Hero.age` is potentially `None`, and you cannot compare `None` with `>`
|
||||||
|
|
||||||
This is because as we are using pure and plain Python annotations for the fields, `age` is indeed annotated as `Optional[int]`, which means `int` or `None`.
|
This is because as we are using pure and plain Python annotations for the fields, `age` is indeed annotated as `int | None (or Optional[int])`.
|
||||||
|
|
||||||
By using this simple and standard Python type annotations we get the benefit of the extra simplicity and the inline error checks when creating or using instances. ✨
|
By using this simple and standard Python type annotations we get the benefit of the extra simplicity and the inline error checks when creating or using instances. ✨
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user