Skip to content

Commit 1d43bd8

Browse files
authored
🐛 Fix pydantic EmailStr support and max_length in several String subclasses (#966)
1 parent 9f3af85 commit 1d43bd8

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

sqlmodel/main.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
overload,
2626
)
2727

28-
from pydantic import BaseModel
28+
from pydantic import BaseModel, EmailStr
2929
from pydantic.fields import FieldInfo as PydanticFieldInfo
3030
from sqlalchemy import (
3131
Boolean,
@@ -574,7 +574,18 @@ def get_sqlalchemy_type(field: Any) -> Any:
574574
# Check enums first as an enum can also be a str, needed by Pydantic/FastAPI
575575
if issubclass(type_, Enum):
576576
return sa_Enum(type_)
577-
if issubclass(type_, str):
577+
if issubclass(
578+
type_,
579+
(
580+
str,
581+
ipaddress.IPv4Address,
582+
ipaddress.IPv4Network,
583+
ipaddress.IPv6Address,
584+
ipaddress.IPv6Network,
585+
Path,
586+
EmailStr,
587+
),
588+
):
578589
max_length = getattr(metadata, "max_length", None)
579590
if max_length:
580591
return AutoString(length=max_length)
@@ -600,16 +611,6 @@ def get_sqlalchemy_type(field: Any) -> Any:
600611
precision=getattr(metadata, "max_digits", None),
601612
scale=getattr(metadata, "decimal_places", None),
602613
)
603-
if issubclass(type_, ipaddress.IPv4Address):
604-
return AutoString
605-
if issubclass(type_, ipaddress.IPv4Network):
606-
return AutoString
607-
if issubclass(type_, ipaddress.IPv6Address):
608-
return AutoString
609-
if issubclass(type_, ipaddress.IPv6Network):
610-
return AutoString
611-
if issubclass(type_, Path):
612-
return AutoString
613614
if issubclass(type_, uuid.UUID):
614615
return GUID
615616
raise ValueError(f"{type_} has no matching SQLAlchemy type")

0 commit comments

Comments
 (0)