Skip to content

Commit 42e6e10

Browse files
committed
Add generics support
1 parent e36e2ea commit 42e6e10

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

sqlmodel/main.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,24 @@ def get_sqlalchemy_type(field: Any) -> Any:
738738
raise ValueError(f"{type_} has no matching SQLAlchemy type")
739739

740740

741+
def _create_union(args: tuple[Any, ...]) -> Any | Any:
742+
if len(args) == 1:
743+
return args[0]
744+
return args[0] | _create_union(args[1:])
745+
746+
741747
def get_column_from_field(field: Any) -> Column: # type: ignore
748+
if isinstance(field.annotation, TypeVar):
749+
generic: TypeVar = field.annotation
750+
if generic.__bound__ is not None:
751+
field.annotation = generic.__bound__
752+
elif generic.__constraints__ != ():
753+
constraints = generic.__constraints__
754+
field.annotation = _create_union(constraints)
755+
else:
756+
raise TypeError(
757+
f"Invalid type used for {field}. Please define a bound or constraints."
758+
)
742759
field_info = field
743760
sa_column = _get_sqlmodel_field_value(field_info, "sa_column", Undefined)
744761
if isinstance(sa_column, Column):

0 commit comments

Comments
 (0)