mirror of
https://github.com/fastapi/sqlmodel.git
synced 2025-08-14 17:41:37 +08:00
✨ Add source examples for Python 3.10 and 3.9 with updated syntax (#842)
Co-authored-by: Esteban Maya Cadavid <emayacadavid9@gmail.com>
This commit is contained in:

committed by
GitHub

parent
4c3f242ae2
commit
9141c8a920
@ -41,18 +41,44 @@ That's why this package is called `SQLModel`. Because it's mainly used to create
|
||||
|
||||
For that, we will import `SQLModel` (plus other things we will also use) and create a class `Hero` that inherits from `SQLModel` and represents the **table model** for our heroes:
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python hl_lines="1 4"
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001_py310.py[ln:1-8]!}
|
||||
|
||||
# More code here later 👇
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.7+
|
||||
|
||||
```Python hl_lines="3 6"
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001.py[ln:1-10]!}
|
||||
|
||||
# More code here later 👇
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001_py310.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.7+
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
///
|
||||
|
||||
This class `Hero` **represents the table** for our heroes. And each instance we create later will **represent a row** in the table.
|
||||
@ -75,18 +101,44 @@ The name of each of these variables will be the name of the column in the table.
|
||||
|
||||
And the type of each of them will also be the type of table column:
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python hl_lines="1 5-8"
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001_py310.py[ln:1-8]!}
|
||||
|
||||
# More code here later 👇
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.7+
|
||||
|
||||
```Python hl_lines="1 3 7-10"
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001.py[ln:1-10]!}
|
||||
|
||||
# More code here later 👇
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001_py310.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.7+
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
///
|
||||
|
||||
Let's now see with more detail these field/column declarations.
|
||||
@ -101,18 +153,44 @@ That is the standard way to declare that something "could be an `int` or `None`"
|
||||
|
||||
And we also set the default value of `age` to `None`.
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python hl_lines="8"
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001_py310.py[ln:1-8]!}
|
||||
|
||||
# More code here later 👇
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.7+
|
||||
|
||||
```Python hl_lines="1 10"
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001.py[ln:1-10]!}
|
||||
|
||||
# More code here later 👇
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001_py310.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.7+
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
///
|
||||
|
||||
/// tip
|
||||
@ -143,18 +221,44 @@ So, we need to mark `id` as the **primary key**.
|
||||
|
||||
To do that, we use the special `Field` function from `sqlmodel` and set the argument `primary_key=True`:
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python hl_lines="1 5"
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001_py310.py[ln:1-8]!}
|
||||
|
||||
# More code here later 👇
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.7+
|
||||
|
||||
```Python hl_lines="3 7"
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001.py[ln:1-10]!}
|
||||
|
||||
# More code here later 👇
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001_py310.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.7+
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
///
|
||||
|
||||
That way, we tell **SQLModel** that this `id` field/column is the primary key of the table.
|
||||
@ -198,18 +302,44 @@ If you have a server database (for example PostgreSQL or MySQL), the **engine**
|
||||
|
||||
Creating the **engine** is very simple, just call `create_engine()` with a URL for the database to use:
|
||||
|
||||
```Python hl_lines="3 16"
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001.py[ln:1-16]!}
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python hl_lines="1 14"
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001_py310.py[ln:1-16]!}
|
||||
|
||||
# More code here later 👇
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.7+
|
||||
|
||||
```Python hl_lines="3 16"
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001.py[ln:1-18]!}
|
||||
|
||||
# More code here later 👇
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001_py310.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.7+
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
///
|
||||
|
||||
You should normally have a single **engine** object for your whole application and re-use it everywhere.
|
||||
@ -234,18 +364,44 @@ SQLite supports a special database that lives all *in memory*. Hence, it's very
|
||||
|
||||
* `sqlite://`
|
||||
|
||||
```Python hl_lines="13-14 16"
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001.py[ln:1-19]!}
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python hl_lines="11-12 14"
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001_py310.py[ln:1-16]!}
|
||||
|
||||
# More code here later 👇
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.7+
|
||||
|
||||
```Python hl_lines="13-14 16"
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001.py[ln:1-18]!}
|
||||
|
||||
# More code here later 👇
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001_py310.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.7+
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
///
|
||||
|
||||
You can read a lot more about all the databases supported by **SQLAlchemy** (and that way supported by **SQLModel**) in the <a href="https://docs.sqlalchemy.org/en/14/core/engines.html" class="external-link" target="_blank">SQLAlchemy documentation</a>.
|
||||
@ -258,18 +414,44 @@ It will make the engine print all the SQL statements it executes, which can help
|
||||
|
||||
It is particularly useful for **learning** and **debugging**:
|
||||
|
||||
```Python hl_lines="16"
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001.py[ln:1-16]!}
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python hl_lines="14"
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001_py310.py[ln:1-16]!}
|
||||
|
||||
# More code here later 👇
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.7+
|
||||
|
||||
```Python hl_lines="16"
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001.py[ln:1-18]!}
|
||||
|
||||
# More code here later 👇
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001_py310.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.7+
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
///
|
||||
|
||||
But in production, you would probably want to remove `echo=True`:
|
||||
@ -296,10 +478,22 @@ And SQLModel's version of `create_engine()` is type annotated internally, so you
|
||||
|
||||
Now everything is in place to finally create the database and table:
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python hl_lines="16"
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001_py310.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.7+
|
||||
|
||||
```Python hl_lines="18"
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
/// tip
|
||||
|
||||
Creating the engine doesn't create the `database.db` file.
|
||||
@ -411,10 +605,22 @@ Put the code it in a file `app.py` if you haven't already.
|
||||
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001_py310.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.7+
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial001.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
///
|
||||
|
||||
/// tip
|
||||
@ -520,18 +726,44 @@ In this example it's just the `SQLModel.metadata.create_all(engine)`.
|
||||
|
||||
Let's put it in a function `create_db_and_tables()`:
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python hl_lines="17-18"
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial002_py310.py[ln:1-18]!}
|
||||
|
||||
# More code here later 👇
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.7+
|
||||
|
||||
```Python hl_lines="19-20"
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial002.py[ln:1-20]!}
|
||||
|
||||
# More code here later 👇
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial002_py310.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.7+
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial002.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
///
|
||||
|
||||
If `SQLModel.metadata.create_all(engine)` was not in a function and we tried to import something from this module (from this file) in another, it would try to create the database and table **every time** we executed that other file that imported this module.
|
||||
@ -562,10 +794,22 @@ The word **script** often implies that the code could be run independently and e
|
||||
|
||||
For that we can use the special variable `__name__` in an `if` block:
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```Python hl_lines="21-22"
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial002_py310.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.7+
|
||||
|
||||
```Python hl_lines="23-24"
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial002.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
### About `__name__ == "__main__"`
|
||||
|
||||
The main purpose of the `__name__ == "__main__"` is to have some code that is executed when your file is called with:
|
||||
@ -658,12 +902,26 @@ But now we can import things from this module in other files.
|
||||
|
||||
Now, let's give the code a final look:
|
||||
|
||||
//// tab | Python 3.10+
|
||||
|
||||
```{.python .annotate}
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial003_py310.py!}
|
||||
```
|
||||
|
||||
{!./docs_src/tutorial/create_db_and_table/annotations/en/tutorial003.md!}
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.7+
|
||||
|
||||
```{.python .annotate}
|
||||
{!./docs_src/tutorial/create_db_and_table/tutorial003.py!}
|
||||
```
|
||||
|
||||
{!./docs_src/tutorial/create_db_and_table/annotations/en/tutorial003.md!}
|
||||
|
||||
////
|
||||
|
||||
/// tip
|
||||
|
||||
Review what each line does by clicking each number bubble in the code. 👆
|
||||
|
Reference in New Issue
Block a user