Skip to content

Commit e8511ef

Browse files
committed
Only check for Pydantic Types with Pydantic V2
1 parent dc96203 commit e8511ef

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

sqlmodel/main.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,8 @@
2626
)
2727

2828
from pydantic import (
29-
AwareDatetime,
3029
BaseModel,
3130
EmailStr,
32-
FutureDate,
33-
FutureDatetime,
34-
NaiveDatetime,
35-
PastDate,
36-
PastDatetime,
3731
)
3832
from pydantic.fields import FieldInfo as PydanticFieldInfo
3933
from sqlalchemy import (
@@ -91,6 +85,16 @@
9185
)
9286
from .sql.sqltypes import AutoString
9387

88+
if IS_PYDANTIC_V2:
89+
from pydantic import (
90+
AwareDatetime,
91+
FutureDate,
92+
FutureDatetime,
93+
NaiveDatetime,
94+
PastDate,
95+
PastDatetime,
96+
)
97+
9498
if TYPE_CHECKING:
9599
from pydantic._internal._model_construction import ModelMetaclass as ModelMetaclass
96100
from pydantic._internal._repr import Representation as Representation
@@ -689,18 +693,23 @@ def get_sqlalchemy_type(field: Any) -> Any:
689693
return Boolean
690694
if issubclass(type_, int):
691695
return Integer
692-
if issubclass(type_, (datetime, FutureDatetime, PastDatetime)):
696+
if issubclass(type_, datetime):
693697
return DateTime
694-
if issubclass(type_, (date, FutureDate, PastDate)):
698+
if issubclass(type_, date):
695699
return Date
696700
if issubclass(type_, timedelta):
697701
return Interval
698702
if issubclass(type_, time):
699703
return Time
700-
if issubclass(type_, AwareDatetime):
701-
return DateTime(timezone=True)
702-
if issubclass(type_, NaiveDatetime):
703-
return DateTime(timezone=False)
704+
if IS_PYDANTIC_V2:
705+
if issubclass(type_, (FutureDate, PastDate)):
706+
return DateTime
707+
if issubclass(type_, (FutureDatetime, PastDatetime)):
708+
return DateTime
709+
if issubclass(type_, AwareDatetime):
710+
return DateTime(timezone=True)
711+
if issubclass(type_, NaiveDatetime):
712+
return DateTime(timezone=False)
704713
if issubclass(type_, bytes):
705714
return LargeBinary
706715
if issubclass(type_, Decimal):

0 commit comments

Comments
 (0)