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
{{ message }}
Repository navigation
Commit 92af074
Browse filesBrowse the repository at this point in the historyBrowse files
The [`UTC` constant](https://docs.python.org/3/library/datetime.html#datetime.UTC) is available on Python 3.11 and newer. It is an alias for `timezone.utc`.
29
-
30
-
For Python 3.10, expand **Other versions and variants** below the example. That version imports `timezone` and uses `datetime.now(timezone.utc)` instead.
28
+
The [`UTC` constant](https://docs.python.org/3/library/datetime.html#datetime.UTC) is an alias for `timezone.utc`.
31
29
32
30
///
33
31
@@ -39,15 +37,15 @@ Passing `datetime.now(UTC)` directly would call it immediately, when defining th
39
37
40
38
We can create an event without passing a value for `created_at`:
For the database column, **SQLModel** uses `UTCDateTime`, a custom SQLAlchemy type based on `DateTime(timezone=True)`. It uses native timezone support when the database provides it.
45
43
46
44
### Write and Read Datetimes
47
45
48
46
Let's save the event to `database.db` and read it back using SQLite:
Copy file name to clipboardExpand all lines: docs_src/tutorial/create_db_and_table/annotations/en/tutorial003.md
+16-17Lines changed: 16 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,44 +1,43 @@
1
-
1. Import `Optional` from `typing` to declare fields that could be `None`.
2
-
2. Import the things we will need from `sqlmodel`: `Field`, `SQLModel`, `create_engine`.
3
-
3. Create the `Hero` model class, representing the `hero` table in the database.
1
+
1. Import the things we will need from `sqlmodel`: `Field`, `SQLModel`, `create_engine`.
2
+
2. Create the `Hero` model class, representing the `hero` table in the database.
4
3
5
4
And also mark this class as a **table model** with `table=True`.
6
5
7
-
4. Create the `id` field:
6
+
3. Create the `id` field:
8
7
9
-
It could be `None` until the database assigns a value to it, so we annotate it with `Optional` (`int | None` in Python 3.10+).
8
+
It could be `None` until the database assigns a value to it, so we annotate it with `int | None`.
10
9
11
10
It is a **primary key**, so we use `Field()` and the argument `primary_key=True`.
12
11
13
-
5. Create the `name` field.
12
+
4. Create the `name` field.
14
13
15
-
It is required, so there's no default value, and it's not `Optional`.
14
+
It is required, so there's no default value, and it cannot be `None`.
16
15
17
-
6. Create the `secret_name` field.
16
+
5. Create the `secret_name` field.
18
17
19
18
Also required.
20
19
21
-
7. Create the `age` field.
20
+
6. Create the `age` field.
22
21
23
22
It is not required, the default value is `None`.
24
23
25
24
In the database, the default value will be `NULL`, the SQL equivalent of `None`.
26
25
27
-
As this field could be `None` (and `NULL` in the database), we annotate it with `Optional` (`int | None` in Python 3.10+).
26
+
As this field could be `None` (and `NULL` in the database), we annotate it with `int | None`.
28
27
29
-
8. Write the name of the database file.
30
-
9. Use the name of the database file to create the database URL.
31
-
10. Create the engine using the URL.
28
+
7. Write the name of the database file.
29
+
8. Use the name of the database file to create the database URL.
30
+
9. Create the engine using the URL.
32
31
33
32
This doesn't create the database yet, no file or table is created at this point, only the **engine** object that will handle the connections with this specific database, and with specific support for SQLite (based on the URL).
34
33
35
-
11. Put the code that creates side effects in a function.
34
+
10. Put the code that creates side effects in a function.
36
35
37
36
In this case, only one line that creates the database file with the table.
38
37
39
-
12. Create all the tables that were automatically registered in `SQLModel.metadata`.
38
+
11. Create all the tables that were automatically registered in `SQLModel.metadata`.
40
39
41
-
13. Add a main block, or "Top-level script environment".
40
+
12. Add a main block, or "Top-level script environment".
42
41
43
42
And put some logic to be executed when this is called directly with Python, as in:
44
43
@@ -58,7 +57,7 @@
58
57
from app import Hero
59
58
```
60
59
61
-
14. In this main block, call the function that creates the database file and the table.
60
+
13. In this main block, call the function that creates the database file and the table.
0 commit comments