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!}
-```
-
-////
-
-///
+{* ./docs_src/tutorial/create_db_and_table/tutorial001_py310.py ln[1:8] hl[1,4] *}
This class `Hero` **represents the table** for our heroes. And each instance we create later will **represent a row** 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!}
-```
-
-////
-
-///
+{* ./docs_src/tutorial/create_db_and_table/tutorial001_py310.py ln[1:8] hl[1,5:8] *}
Let's now see with more detail these field/column declarations.
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!}
-```
-
-////
-
-///
+{* ./docs_src/tutorial/create_db_and_table/tutorial001_py310.py ln[1:8] hl[8] *}
/// tip
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!}
-```
-
-////
-
-///
+{* ./docs_src/tutorial/create_db_and_table/tutorial001_py310.py ln[1:8] hl[1,5] *}
That way, we tell **SQLModel** that this `id` field/column is the primary key of the table.
Creating the **engine** is very simple, just call `create_engine()` with a URL for the database to use:
-//// 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!}
-```
-
-////
-
-///
+{* ./docs_src/tutorial/create_db_and_table/tutorial001_py310.py ln[1:16] hl[1,14] *}
You should normally have a single **engine** object for your whole application and re-use it everywhere.
* `sqlite://`
-//// 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!}
-```
-
-////
-
-///
+{* ./docs_src/tutorial/create_db_and_table/tutorial001_py310.py ln[1:16] hl[11:12,14] *}
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>.
It is particularly useful for **learning** and **debugging**:
-//// 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!}
-```
-
-////
-
-///
+{* ./docs_src/tutorial/create_db_and_table/tutorial001_py310.py ln[1:16] hl[14] *}
But in production, you would probably want to remove `echo=True`:
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!}
-```
-
-////
+{* ./docs_src/tutorial/create_db_and_table/tutorial001_py310.py hl[16] *}
/// tip
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!}
-```
-
-////
-
-///
+{* ./docs_src/tutorial/create_db_and_table/tutorial001_py310.py *}
/// tip
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!}
-```
-
-////
-
-///
+{* ./docs_src/tutorial/create_db_and_table/tutorial002_py310.py ln[1:18] hl[17:18] *}
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.
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!}
-```
-
-////
+{* ./docs_src/tutorial/create_db_and_table/tutorial002_py310.py hl[21:22] *}
### About `__name__ == "__main__"`