Skip to content

✨ Add support for pydantic date types #1384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion sqlmodel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
overload,
)

from pydantic import BaseModel, EmailStr
from pydantic import (
BaseModel,
EmailStr,
)
from pydantic.fields import FieldInfo as PydanticFieldInfo
from sqlalchemy import (
Boolean,
Expand Down Expand Up @@ -82,6 +85,16 @@
)
from .sql.sqltypes import AutoString

if IS_PYDANTIC_V2:
from pydantic import (
AwareDatetime,
FutureDate,
FutureDatetime,
NaiveDatetime,
PastDate,
PastDatetime,
)

if TYPE_CHECKING:
from pydantic._internal._model_construction import ModelMetaclass as ModelMetaclass
from pydantic._internal._repr import Representation as Representation
Expand Down Expand Up @@ -688,6 +701,15 @@ def get_sqlalchemy_type(field: Any) -> Any:
return Interval
if issubclass(type_, time):
return Time
if IS_PYDANTIC_V2:
if issubclass(type_, (FutureDate, PastDate)):
return DateTime
if issubclass(type_, (FutureDatetime, PastDatetime)):
return DateTime
if issubclass(type_, AwareDatetime):
return DateTime(timezone=True)
if issubclass(type_, NaiveDatetime):
return DateTime(timezone=False)
if issubclass(type_, bytes):
return LargeBinary
if issubclass(type_, Decimal):
Expand Down
Loading