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
SQLModel is a library for interacting with <abbrtitle='Also called "Relational databases"'>SQL databases</abbr> from Python code, with Python objects. It is designed to be intuitive, easy to use, highly compatible, and robust.
31
31
32
-
**SQLModel** is based on Python type annotations, and powered by <ahref="https://pydantic-docs.helpmanual.io/"class="external-link"target="_blank">Pydantic</a> and <ahref="https://sqlalchemy.org/"class="external-link"target="_blank">SQLAlchemy</a>.
32
+
**SQLModel** is based on Python type annotations, and powered by [Pydantic](https://pydantic-docs.helpmanual.io/)and [SQLAlchemy](https://sqlalchemy.org/).
**SQLModel** is designed to simplify interacting with SQL databases in <ahref="https://fastapi.tiangolo.com"class="external-link"target="_blank">FastAPI</a> applications, it was created by the same <ahref="https://tiangolo.com/"class="external-link"target="_blank">author</a>. 😁
54
+
**SQLModel** is designed to simplify interacting with SQL databases in [FastAPI](https://fastapi.tiangolo.com)applications, it was created by the same [author](https://tiangolo.com/). 😁
55
55
56
56
It combines SQLAlchemy and Pydantic and tries to simplify the code you write as much as possible, allowing you to reduce the **code duplication to a minimum**, but while getting the **best developer experience** possible.
57
57
58
58
**SQLModel** is, in fact, a thin layer on top of **Pydantic** and **SQLAlchemy**, carefully designed to be compatible with both.
59
59
60
60
## Requirements
61
61
62
-
A recent and currently supported <ahref="https://www.python.org/downloads/"class="external-link"target="_blank">version of Python</a>.
62
+
A recent and currently supported [version of Python](https://www.python.org/downloads/).
63
63
64
64
As **SQLModel** is based on **Pydantic** and **SQLAlchemy**, it requires them. They will be automatically installed when you install SQLModel.
65
65
66
66
## Installation
67
67
68
-
Make sure you create a <ahref="https://sqlmodel.tiangolo.com/virtual-environments/"class="external-link"target="_blank">virtual environment</a>, activate it, and then install SQLModel, for example with:
68
+
Make sure you create a [virtual environment](https://sqlmodel.tiangolo.com/virtual-environments/), activate it, and then install SQLModel, for example with:
69
69
70
70
<divclass="termy">
71
71
@@ -79,7 +79,7 @@ Successfully installed sqlmodel
79
79
80
80
## Example
81
81
82
-
For an introduction to databases, SQL, and everything else, see the <ahref="https://sqlmodel.tiangolo.com/databases/"target="_blank">SQLModel documentation</a>.
82
+
For an introduction to databases, SQL, and everything else, see the [SQLModel documentation](https://sqlmodel.tiangolo.com/databases/).
Copy file name to clipboardExpand all lines: docs/advanced/decimal.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,21 +11,21 @@ As an example, if you open Python and sum `1.1` + `2.2` you would expect to see
11
11
3.3000000000000003
12
12
```
13
13
14
-
This is because of the way numbers are stored in "ones and zeros" (binary). But Python has a module and some types to have strict decimal values. You can read more about it in the official <ahref="https://docs.python.org/3/library/decimal.html"class="external-link"target="_blank">Python docs for Decimal</a>.
14
+
This is because of the way numbers are stored in "ones and zeros" (binary). But Python has a module and some types to have strict decimal values. You can read more about it in the official [Python docs for Decimal](https://docs.python.org/3/library/decimal.html).
15
15
16
16
Because databases store data in the same ways as computers (in binary), they would have the same types of issues. And because of that, they also have a special **decimal** type.
17
17
18
18
In most cases this would probably not be a problem, for example measuring views in a video, or the life bar in a videogame. But as you can imagine, this is particularly important when dealing with **money** and **finances**.
19
19
20
20
## Decimal Types
21
21
22
-
Pydantic has special support for <ahref="https://docs.pydantic.dev/latest/api/standard_library_types/#decimaldecimal"class="external-link"target="_blank">`Decimal` types</a>.
22
+
Pydantic has special support for [`Decimal` types](https://docs.pydantic.dev/latest/api/standard_library_types/#decimaldecimal).
23
23
24
24
When you use `Decimal` you can specify the number of digits and decimal places to support in the `Field()` function. They will be validated by Pydantic (for example when using FastAPI) and the same information will also be used for the database columns.
25
25
26
26
/// info
27
27
28
-
For the database, **SQLModel** will use <ahref="https://docs.sqlalchemy.org/en/20/core/type_basics.html#sqlalchemy.types.DECIMAL"class="external-link"target="_blank">SQLAlchemy's `DECIMAL` type</a>.
28
+
For the database, **SQLModel** will use [SQLAlchemy's `DECIMAL` type](https://docs.sqlalchemy.org/en/20/core/type_basics.html#sqlalchemy.types.DECIMAL).
Pydantic has support for <ahref="https://docs.pydantic.dev/latest/api/standard_library_types/#uuid"class="external-link"target="_blank">`UUID` types</a>.
85
+
Pydantic has support for [`UUID` types](https://docs.pydantic.dev/latest/api/standard_library_types/#uuid).
86
86
87
-
For the database, **SQLModel** internally uses <ahref="https://docs.sqlalchemy.org/en/20/core/type_basics.html#sqlalchemy.types.Uuid"class="external-link"target="_blank">SQLAlchemy's `Uuid` type</a>.
87
+
For the database, **SQLModel** internally uses [SQLAlchemy's `Uuid` type](https://docs.sqlalchemy.org/en/20/core/type_basics.html#sqlalchemy.types.Uuid).
88
88
89
89
### Create a Record with a UUID
90
90
@@ -174,5 +174,5 @@ Selected hero ID:
174
174
175
175
You can learn more about **UUIDs** in:
176
176
177
-
* The official <ahref="https://docs.python.org/3/library/uuid.html"class="external-link"target="_blank">Python docs for UUID</a>.
178
-
* The <ahref="https://en.wikipedia.org/wiki/Universally_unique_identifier"class="external-link"target="_blank">Wikipedia for UUID</a>.
177
+
* The official [Python docs for UUID](https://docs.python.org/3/library/uuid.html).
178
+
* The [Wikipedia for UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier).
Copy file name to clipboardExpand all lines: docs/contributing.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
# Contributing
2
2
3
-
First, you might want to see the basic ways to [help SQLModel and get help](help.md){.internal-link target=_blank}.
3
+
First, you might want to see the basic ways to [help SQLModel and get help](help.md).
4
4
5
5
## Developing
6
6
7
-
If you already cloned the <ahref="https://github.com/fastapi/sqlmodel"class="external-link"target="_blank">sqlmodel repository</a> and you want to deep dive in the code, here are some guidelines to set up your environment.
7
+
If you already cloned the [sqlmodel repository](https://github.com/fastapi/sqlmodel) and you want to deep dive in the code, here are some guidelines to set up your environment.
The instructions here show you how to use the script at `./scripts/docs.py` with the `python` program directly.
114
114
115
-
But you can also use <ahref="https://typer.tiangolo.com/typer-cli/"class="external-link"target="_blank">Typer CLI</a>, and you will get autocompletion in your terminal for the commands after installing completion.
115
+
But you can also use [Typer CLI](https://typer.tiangolo.com/typer-cli/), and you will get autocompletion in your terminal for the commands after installing completion.
116
116
117
117
If you install Typer CLI, you can install completion with:
118
118
@@ -129,7 +129,7 @@ Completion will take effect once you restart the terminal.
129
129
130
130
### Docs Structure
131
131
132
-
The documentation uses <ahref="https://www.mkdocs.org/"class="external-link"target="_blank">MkDocs</a>.
132
+
The documentation uses [MkDocs](https://www.mkdocs.org/).
133
133
134
134
And there are extra tools/scripts in place in `./scripts/docs.py`.
135
135
@@ -175,7 +175,7 @@ The same applies to comments and descriptions, please don't copy paste the conte
175
175
176
176
### Human Effort Denial of Service
177
177
178
-
Using automated tools and AI to submit PRs or comments that we have to carefully review and handle would be the equivalent of a <ahref="https://en.wikipedia.org/wiki/Denial-of-service_attack"class="external-link"target="_blank">Denial-of-service attack</a> on our human effort.
178
+
Using automated tools and AI to submit PRs or comments that we have to carefully review and handle would be the equivalent of a [Denial-of-service attack](https://en.wikipedia.org/wiki/Denial-of-service_attack) on our human effort.
179
179
180
180
It would be very little effort from the person submitting the PR (an LLM prompt) that generates a large amount of effort on our side (carefully reviewing code).
Copy file name to clipboardExpand all lines: docs/databases.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -319,7 +319,7 @@ I'll tell you more about SQL, SQLModel, how to use them, and how they are relate
319
319
320
320
/// info | Technical Details
321
321
322
-
SQLModel is built on top of SQLAlchemy. It is, in fact, just <ahref="https://www.sqlalchemy.org/"class="external-link"target="_blank">SQLAlchemy</a> and <ahref="https://pydantic-docs.helpmanual.io/"class="external-link"target="_blank">Pydantic</a> mixed together with some sugar on top.
322
+
SQLModel is built on top of SQLAlchemy. It is, in fact, just [SQLAlchemy](https://www.sqlalchemy.org/)and [Pydantic](https://pydantic-docs.helpmanual.io/) mixed together with some sugar on top.
Copy file name to clipboardExpand all lines: docs/db-to-code.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -111,15 +111,15 @@ DROP TABLE hero;
111
111
112
112
That is how you tell the database in SQL to delete the entire table `hero`.
113
113
114
-
<ahref="https://theuselessweb.site/nooooooooooooooo/"class="external-link"target="_blank">Nooooo!</a> We lost all the data in the `hero` table! 💥😱
114
+
[Nooooo!](https://theuselessweb.site/nooooooooooooooo/) We lost all the data in the `hero` table! 💥😱
115
115
116
116
### SQL Sanitization
117
117
118
118
The process of making sure that whatever the external user sends is safe to use in the SQL string is called **sanitization**.
119
119
120
120
It comes by default in **SQLModel** (thanks to SQLAlchemy). And many other similar tools would also provide that functionality among many other features.
121
121
122
-
Now you are ready for <ahref="https://xkcd.com/327/"class="external-link"target="_blank">a joke from xkcd</a>:
122
+
Now you are ready for [a joke from xkcd](https://xkcd.com/327/):
123
123
124
124

125
125
@@ -292,7 +292,7 @@ It's actually a simple idea with a very academic and mathematical name. 😅
292
292
293
293
So, an **ORM** is a library that translates from SQL to code, and from code to SQL. All using classes and objects.
294
294
295
-
There are many ORMs available apart from **SQLModel**, you can read more about some of them in [Alternatives, Inspiration and Comparisons](alternatives.md){.internal-link target=_blank}
295
+
There are many ORMs available apart from **SQLModel**, you can read more about some of them in [Alternatives, Inspiration and Comparisons](alternatives.md)
Copy file name to clipboardExpand all lines: docs/environment-variables.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -67,7 +67,7 @@ print(f"Hello {name} from Python")
67
67
68
68
/// tip
69
69
70
-
The second argument to <ahref="https://docs.python.org/3.8/library/os.html#os.getenv"class="external-link"target="_blank">`os.getenv()`</a> is the default value to return.
70
+
The second argument to [`os.getenv()`](https://docs.python.org/3.8/library/os.html#os.getenv) is the default value to return.
71
71
72
72
If not provided, it's `None` by default, here we provide `"World"` as the default value to use.
73
73
@@ -155,7 +155,7 @@ Hello World from Python
155
155
156
156
/// tip
157
157
158
-
You can read more about it at <ahref="https://12factor.net/config"class="external-link"target="_blank">The Twelve-Factor App: Config</a>.
158
+
You can read more about it at [The Twelve-Factor App: Config](https://12factor.net/config).
This information will be useful when learning about [Virtual Environments](virtual-environments.md){.internal-link target=_blank}.
290
+
This information will be useful when learning about [Virtual Environments](virtual-environments.md).
291
291
292
292
## Conclusion
293
293
294
294
With this you should have a basic understanding of what **environment variables** are and how to use them in Python.
295
295
296
-
You can also read more about them in the <ahref="https://en.wikipedia.org/wiki/Environment_variable"class="external-link"target="_blank">Wikipedia for Environment Variable</a>.
296
+
You can also read more about them in the [Wikipedia for Environment Variable](https://en.wikipedia.org/wiki/Environment_variable).
297
297
298
298
In many cases it's not very obvious how environment variables would be useful and applicable right away. But they keep showing up in many different scenarios when you are developing, so it's good to know about them.
It follows the same design and ideas, and it was created to be the most intuitive way to interact with SQL databases in FastAPI applications.
10
10
@@ -14,29 +14,29 @@ Nevertheless, SQLModel is completely **independent** of FastAPI and can be used
14
14
15
15
It's all based on standard <abbrtitle="Currently supported versions of Python">modern **Python**</abbr> type annotations. No new syntax to learn. Just standard modern Python.
16
16
17
-
If you need a 2 minute refresher of how to use Python types (even if you don't use SQLModel or FastAPI), check the FastAPI tutorial section: <ahref="https://fastapi.tiangolo.com/python-types/"class="external-link"target="_blank">Python types intro</a>.
17
+
If you need a 2 minute refresher of how to use Python types (even if you don't use SQLModel or FastAPI), check the FastAPI tutorial section: [Python types intro](https://fastapi.tiangolo.com/python-types/).
18
18
19
-
You will also see a 20 seconds refresher on the section [Tutorial - User Guide: First Steps](tutorial/index.md){.internal-link target=_blank}.
19
+
You will also see a 20 seconds refresher on the section [Tutorial - User Guide: First Steps](tutorial/index.md).
20
20
21
21
## Editor support
22
22
23
23
**SQLModel** was designed to be easy and intuitive to use to ensure the best development experience, with autocompletion everywhere.
24
24
25
25
Here's how your editor might help you:
26
26
27
-
* in <ahref="https://code.visualstudio.com/"class="external-link"target="_blank">Visual Studio Code</a>:
27
+
* in [Visual Studio Code](https://code.visualstudio.com/):
You will get completion for everything while writing the **minimum** amount of code.
36
36
37
37
You won't need to keep guessing the types of different attributes in your models, if they could be `None`, etc. Your editor will be able to help you with everything because **SQLModel** is based on **standard Python type annotations**.
38
38
39
-
**SQLModel** adopts <ahref="https://peps.python.org/pep-0681/"class="external-link"target="_blank">PEP 681</a> for Python type annotations to ensure the **best developer experience**, so you will get inline errors and autocompletion even while creating new model instances.
39
+
**SQLModel** adopts [PEP 681](https://peps.python.org/pep-0681/) for Python type annotations to ensure the **best developer experience**, so you will get inline errors and autocompletion even while creating new model instances.
@@ -76,7 +76,7 @@ Underneath, ✨ a **SQLModel** model is also a **SQLAlchemy** model. ✨
76
76
77
77
There was **a lot** of research and effort dedicated to make it that way. In particular, there was a lot of effort and experimentation in making a single model be **both a SQLAlchemy model and a Pydantic** model at the same time.
78
78
79
-
That means that you get all the power, robustness, and certainty of SQLAlchemy, the <ahref="https://lp.jetbrains.com/python-developers-survey-2024/#orms"class="external-link"target="_blank">most widely used database library in Python</a>.
79
+
That means that you get all the power, robustness, and certainty of SQLAlchemy, the [most widely used database library in Python](https://lp.jetbrains.com/python-developers-survey-2024/#orms).
80
80
81
81
**SQLModel** provides its own utilities to <abbrtitle="with type completion, type checks, etc.">improve the developer experience</abbr>, but underneath, it uses all of SQLAlchemy.
0 commit comments