Skip to content

Commit 8bd9068

Browse files
committed
Add support for pydantic.EmailStr
1 parent 0cdc1b1 commit 8bd9068

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

docs/advanced/column-types.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,22 @@ In addition, the following types are stored as `VARCHAR`:
112112
* ipaddress.IPv6Address
113113
* ipaddress.IPv6Network
114114
* pathlib.Path
115+
* pydantic.EmailStr
115116

116117
### IP Addresses
117118

118119
IP Addresses from the <a href="https://docs.python.org/3/library/ipaddress.html" class="external-link" target="_blank">Python `ipaddress` module</a> are stored as text.
119120

120-
```Python hl_lines="1 11"
121-
{!./docs_src/advanced/column_types/tutorial003.py[ln:1-13]!}
121+
```Python hl_lines="1 12"
122+
{!./docs_src/advanced/column_types/tutorial003.py[ln:1-15]!}
122123
```
123124

124125
### Filesystem Paths
125126

126127
Paths to files and directories using the <a href="https://docs.python.org/3/library/pathlib.html" class="external-link" target="_blank">Python `pathlib` module</a> are stored as text.
127128

128-
```Python hl_lines="3 12"
129-
{!./docs_src/advanced/column_types/tutorial003.py[ln:1-13]!}
129+
```Python hl_lines="3 13"
130+
{!./docs_src/advanced/column_types/tutorial003.py[ln:1-15]!}
130131
```
131132

132133
/// tip
@@ -141,8 +142,17 @@ ensure you call `absolute()` on the path before setting it in your model.
141142
UUIDs from the <a href="https://docs.python.org/3/library/uuid.html" class="external-link" target="_blank">Python `uuid`
142143
module</a> are stored as `UUID` types in supported databases (just PostgreSQL at the moment), otherwise as a `CHAR(32)`.
143144

144-
```Python hl_lines="4 10"
145-
{!./docs_src/advanced/column_types/tutorial003.py[ln:1-13]!}
145+
```Python hl_lines="4 11"
146+
{!./docs_src/advanced/column_types/tutorial003.py[ln:1-15]!}
147+
```
148+
149+
### Email Addresses
150+
151+
Email addresses using <a href="https://docs.pydantic.dev/latest/api/networks/#pydantic.networks.EmailStr" class="external-link" target="_blank">Pydantic's `EmailStr` type</a>
152+
are stored as strings.
153+
154+
```Python hl_lines="6 15"
155+
{!./docs_src/advanced/column_types/tutorial003.py[ln:1-15]!}
146156
```
147157

148158
## Custom Pydantic types

docs_src/advanced/column_types/tutorial003.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pathlib import Path
44
from uuid import UUID, uuid4
55

6+
from pydantic import EmailStr
67
from sqlmodel import Field, SQLModel, create_engine
78

89

@@ -11,6 +12,7 @@ class Avatar(SQLModel, table=True):
1112
source_ip_address: ipaddress.IPv4Address
1213
upload_location: Path
1314
uploaded_at: datetime = Field(default=datetime.now(tz=UTC))
15+
author_email: EmailStr
1416

1517

1618
sqlite_file_name = "database.db"

sqlmodel/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
overload,
2525
)
2626

27-
from pydantic import BaseModel
27+
from pydantic import BaseModel, EmailStr
2828
from pydantic.fields import FieldInfo as PydanticFieldInfo
2929
from sqlalchemy import (
3030
Boolean,
@@ -602,6 +602,8 @@ def get_sqlalchemy_type(field: Any) -> Any:
602602
return AutoString
603603
if issubclass(type_, Path):
604604
return AutoString
605+
if issubclass(type_, EmailStr):
606+
return AutoString
605607
if issubclass(type_, uuid.UUID):
606608
return GUID
607609
raise ValueError(f"{type_} has no matching SQLAlchemy type")

0 commit comments

Comments
 (0)