Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 6 additions & 18 deletions docs/tutorial/code-structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ We can use these relative imports because, for example, in the file `app.py` (th

You could put all the database Models in a single Python module (a single Python file), for example `models.py`:

```Python
{!./docs_src/tutorial/code_structure/tutorial001/models.py!}
```
{* ./docs_src/tutorial/code_structure/tutorial001/models.py *}

This way, you wouldn't have to deal with circular imports for other models.

Expand All @@ -79,19 +77,15 @@ And then you could import the models from this file/module in any other file/mod

Then you could put the code creating the **engine** and the function to create all the tables (if you are not using migrations) in another file `database.py`:

```Python
{!./docs_src/tutorial/code_structure/tutorial001/database.py!}
```
{* ./docs_src/tutorial/code_structure/tutorial001/database.py *}

This file would also be imported by your application code, to use the shared **engine** and to get and call the function `create_db_and_tables()`.

### Application File

Finally, you could put the code to create the **app** in another file `app.py`:

```Python hl_lines="3-4"
{!./docs_src/tutorial/code_structure/tutorial001/app.py!}
```
{* ./docs_src/tutorial/code_structure/tutorial001/app.py hl[3:4] *}

Here we import the models, the engine, and the function to create all the tables and then we can use them all internally.

Expand Down Expand Up @@ -207,29 +201,23 @@ So, we can use it in an `if` block and import things inside the `if` block. And

Using that trick of `TYPE_CHECKING` we can "import" the `Team` in `hero_model.py`:

```Python hl_lines="1 5-6 16"
{!./docs_src/tutorial/code_structure/tutorial002/hero_model.py!}
```
{* ./docs_src/tutorial/code_structure/tutorial002/hero_model.py hl[1,5:6,16] *}

Have in mind that now we *have* to put the annotation of `Team` as a string: `"Team"`, so that Python doesn't have errors at runtime.

### Team Model File

We use the same trick in the `team_model.py` file:

```Python hl_lines="1 5-6 14"
{!./docs_src/tutorial/code_structure/tutorial002/team_model.py!}
```
{* ./docs_src/tutorial/code_structure/tutorial002/team_model.py hl[1,5:6,14] *}

Now we get editor support, autocompletion, inline errors, and **SQLModel** keeps working. 🎉

### App File

Now, just for completeness, the `app.py` file would import the models from both modules:

```Python hl_lines="4-5 10 12-14"
{!./docs_src/tutorial/code_structure/tutorial002/app.py!}
```
{* ./docs_src/tutorial/code_structure/tutorial002/app.py hl[4:5,10,12:14] *}

And of course, all the tricks with `TYPE_CHECKING` and type annotations in strings are **only needed in the files with circular imports**.

Expand Down
Loading