Skip to content

Commit 435ad53

Browse files
committed
support PEP 593 for relationships
1 parent 893f8bd commit 435ad53

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

sqlmodel/main.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
from sqlalchemy.orm.instrumentation import is_instrumented
5353
from sqlalchemy.sql.schema import MetaData
5454
from sqlalchemy.sql.sqltypes import LargeBinary, Time, Uuid
55-
from typing_extensions import Literal, TypeAlias, deprecated, get_origin
55+
from typing_extensions import Annotated, Literal, TypeAlias, deprecated, get_args, get_origin
5656

5757
from ._compat import ( # type: ignore[attr-defined]
5858
IS_PYDANTIC_V2,
@@ -475,6 +475,16 @@ def Relationship(
475475
return relationship_info
476476

477477

478+
def get_annotated_relationshipinfo(t: Type) -> RelationshipInfo | None:
479+
"""Get the first RelationshipInfo from Annotated or None if not Annotated with RelationshipInfo."""
480+
if get_origin(t) is not Annotated:
481+
return None
482+
for a in get_args(t):
483+
if isinstance(a, RelationshipInfo):
484+
return a
485+
return None
486+
487+
478488
@__dataclass_transform__(kw_only_default=True, field_descriptors=(Field, FieldInfo))
479489
class SQLModelMetaclass(ModelMetaclass, DeclarativeMeta):
480490
__sqlmodel_relationships__: Dict[str, RelationshipInfo]
@@ -515,7 +525,12 @@ def __new__(
515525
else:
516526
dict_for_pydantic[k] = v
517527
for k, v in original_annotations.items():
518-
if k in relationships:
528+
# check for `field: Annotated[Any, Relationship()]`
529+
t = get_annotated_relationshipinfo(v)
530+
if t:
531+
relationships[k] = t
532+
relationship_annotations[k] = get_args(v)[0]
533+
elif k in relationships:
519534
relationship_annotations[k] = v
520535
else:
521536
pydantic_annotations[k] = v

0 commit comments

Comments
 (0)