mirror of
https://github.com/fastapi/sqlmodel.git
synced 2025-10-31 01:58:00 +08:00
🐛 Fix pydantic EmailStr support and max_length in several String subclasses (#966)
This commit is contained in:
@ -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")
|
||||
|
||||
Reference in New Issue
Block a user