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
Copy file name to clipboardExpand all lines: docs/advanced/column-types.md
+13-28
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,12 @@
1
1
# Column Types
2
2
3
-
In the tutorial, we stored scalar data types in our tables, like strings, numbers and timestamps. In practice, we often
4
-
work with more complicated types that need to be converted to a data type our database supports.
3
+
In the tutorial, we stored scalar data types in our tables, like strings, numbers and timestamps. In practice, we often work with more complicated types that need to be converted to a data type our database supports.
5
4
6
5
## Customising String Field Lengths
7
6
8
-
As we discussed in [`TEXT` or `VARCHAR`](../tutorial/create-db-and-table.md#text-or-varchar), a `str` field type will be
9
-
created as a `VARCHAR`, which has varying maximum-lengths depending on the database engine you are using.
7
+
As we discussed in [`TEXT` or `VARCHAR`](../tutorial/create-db-and-table.md#text-or-varchar), a `str` field type will be created as a `VARCHAR`, which has varying maximum-lengths depending on the database engine you are using.
10
8
11
-
For cases where you know you only need to store a certain length of text, string field maximum length can be reduced
12
-
using the `max_length` validation argument to `Field()`:
9
+
For cases where you know you only need to store a certain length of text, string field maximum length can be reduced using the `max_length` validation argument to `Field()`:
@@ -25,35 +22,28 @@ using the `max_length` validation argument to `Field()`:
25
22
26
23
/// warning
27
24
28
-
Database engines behave differently when you attempt to store longer text than the character length of the `VARCHAR`
29
-
column. Notably:
25
+
Database engines behave differently when you attempt to store longer text than the character length of the `VARCHAR` column. Notably:
30
26
31
27
* SQLite does not enforce the length of a `VARCHAR`. It will happily store up to 500-million characters of text.
32
28
* MySQL will emit a warning, but will also truncate your text to fit the size of the `VARCHAR`.
33
29
* PostgreSQL will respond with an error code, and your query will not be executed.
34
30
35
31
///
36
32
37
-
However if you need to store much longer strings than `VARCHAR` can allow, databases provide `TEXT` or `CLOB`
38
-
(**c**haracter **l**arge **ob**ject) column types. We can use these by specifying an SQLAlchemy column type to the field
39
-
with the `sa_type` keyword argument:
33
+
However if you need to store much longer strings than `VARCHAR` can allow, databases provide `TEXT` or `CLOB` (**c**haracter **l**arge **ob**ject) column types. We can use these by specifying an SQLAlchemy column type to the field with the `sa_type` keyword argument:
`Text` also accepts a character length argument, which databases use to optimise the storage of a particular field.
48
-
Some databases support `TINYTEXT`, `SMALLTEXT`, `MEDIUMTEXT` and `LONGTEXT` column types - ranging from 255 bytes to
49
-
4 gigabytes. If you know the maximum length of data, specifying it like `Text(1000)` will automatically select the
50
-
best-suited, supported type for your database engine.
41
+
`Text` also accepts a character length argument, which databases use to optimise the storage of a particular field. Some databases support `TINYTEXT`, `SMALLTEXT`, `MEDIUMTEXT` and `LONGTEXT` column types - ranging from 255 bytes to 4 gigabytes. If you know the maximum length of data, specifying it like `Text(1000)` will automatically select the best-suited, supported type for your database engine.
51
42
52
43
///
53
44
54
45
55
-
With this approach, we can use [any kind of SQLAlchemy type](https://docs.sqlalchemy.org/en/20/core/type_basics.html).
56
-
For example, if we were building a mapping application, we could store spatial information:
46
+
With this approach, we can use [any kind of SQLAlchemy type](https://docs.sqlalchemy.org/en/20/core/type_basics.html). For example, we can store pickled objects in the database:
@@ -121,40 +111,37 @@ Note that while the column types for these are `VARCHAR`, values are not convert
121
111
122
112
### IP Addresses
123
113
124
-
IP Addresses from the <ahref="https://docs.python.org/3/library/ipaddress.html"class="external-link"target="_blank">Python `ipaddress` module</a> are stored as text.
114
+
IP Addresses from the [Python `ipaddress` module](https://docs.python.org/3/library/ipaddress.html){.external-link target=_blank} are stored as text.
Paths to files and directories using the <ahref="https://docs.python.org/3/library/pathlib.html"class="external-link"target="_blank">Python `pathlib` module</a> are stored as text.
122
+
Paths to files and directories using the [Python `pathlib` module](https://docs.python.org/3/library/pathlib.html){.external-link target=_blank} are stored as text.
The stored value of a Path is the basic string value: `str(Path('../path/to/file'))`. If you need to store the full path
141
-
ensure you call `absolute()` on the path before setting it in your model.
130
+
The stored value of a Path is the basic string value: `str(Path('../path/to/file'))`. If you need to store the full path ensure you call `absolute()` on the path before setting it in your model.
142
131
143
132
///
144
133
145
134
### UUIDs
146
135
147
-
UUIDs from the <ahref="https://docs.python.org/3/library/uuid.html"class="external-link"target="_blank">Python `uuid`
148
-
module</a> are stored as `UUID` types in supported databases (just PostgreSQL at the moment), otherwise as a `CHAR(32)`.
136
+
UUIDs from the [Python `uuid` module](https://docs.python.org/3/library/uuid.html){.external-link target=_blank} are stored as native `UUID` types in supported databases (just PostgreSQL at the moment), otherwise as a `CHAR(32)`.
Email addresses using <ahref="https://docs.pydantic.dev/latest/api/networks/#pydantic.networks.EmailStr"class="external-link"target="_blank">Pydantic's `EmailStr` type</a>
157
-
are stored as strings.
144
+
Email addresses using [Pydantic's `EmailStr` type](https://docs.pydantic.dev/latest/api/networks/#pydantic.networks.EmailStr){.external-link target=_blank} are stored as strings.
As SQLModel is built on Pydantic, you can use any custom type as long as it would work in a Pydantic model. However, if
167
-
the type is not a subclass of [a type from the table above](#supported-types), you will need to specify an SQLAlchemy
168
-
type to use.
153
+
As SQLModel is built on Pydantic, you can use any custom type as long as it would work in a Pydantic model. However, if the type is not a subclass of [a type from the table above](#supported-types), you will need to specify an SQLAlchemy type to use.
0 commit comments