]> git.ipfire.org Git - thirdparty/fastapi/sqlmodel.git/commitdiff
🐛 Fix pydantic `EmailStr` support and `max_length` in several String subclasses ...
authorEsteban Maya <emayacadavid9@gmail.com>
Tue, 4 Jun 2024 02:47:40 +0000 (21:47 -0500)
committerGitHub <noreply@github.com>
Tue, 4 Jun 2024 02:47:40 +0000 (21:47 -0500)
sqlmodel/main.py

index a16428b1921a628e194b0a1d4f9694da4088effa..40051a522c412f22e6420678235c486fb6cd5029 100644 (file)
@@ -25,7 +25,7 @@ from typing import (
     overload,
 )
 
-from pydantic import BaseModel
+from pydantic import BaseModel, EmailStr
 from pydantic.fields import FieldInfo as PydanticFieldInfo
 from sqlalchemy import (
     Boolean,
@@ -574,7 +574,18 @@ def get_sqlalchemy_type(field: Any) -> Any:
     # Check enums first as an enum can also be a str, needed by Pydantic/FastAPI
     if issubclass(type_, Enum):
         return sa_Enum(type_)
-    if issubclass(type_, str):
+    if issubclass(
+        type_,
+        (
+            str,
+            ipaddress.IPv4Address,
+            ipaddress.IPv4Network,
+            ipaddress.IPv6Address,
+            ipaddress.IPv6Network,
+            Path,
+            EmailStr,
+        ),
+    ):
         max_length = getattr(metadata, "max_length", None)
         if max_length:
             return AutoString(length=max_length)
@@ -600,16 +611,6 @@ def get_sqlalchemy_type(field: Any) -> Any:
             precision=getattr(metadata, "max_digits", None),
             scale=getattr(metadata, "decimal_places", None),
         )
-    if issubclass(type_, ipaddress.IPv4Address):
-        return AutoString
-    if issubclass(type_, ipaddress.IPv4Network):
-        return AutoString
-    if issubclass(type_, ipaddress.IPv6Address):
-        return AutoString
-    if issubclass(type_, ipaddress.IPv6Network):
-        return AutoString
-    if issubclass(type_, Path):
-        return AutoString
     if issubclass(type_, uuid.UUID):
         return GUID
     raise ValueError(f"{type_} has no matching SQLAlchemy type")