Skip to content

Commit cbaf172

Browse files
✨ Add support for passing a custom SQLAlchemy type to Field() with sa_type (#505)
Co-authored-by: Sebastián Ramírez <[email protected]>
1 parent c557cf6 commit cbaf172

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

sqlmodel/main.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def __init__(self, default: Any = Undefined, **kwargs: Any) -> None:
7474
foreign_key = kwargs.pop("foreign_key", Undefined)
7575
unique = kwargs.pop("unique", False)
7676
index = kwargs.pop("index", Undefined)
77+
sa_type = kwargs.pop("sa_type", Undefined)
7778
sa_column = kwargs.pop("sa_column", Undefined)
7879
sa_column_args = kwargs.pop("sa_column_args", Undefined)
7980
sa_column_kwargs = kwargs.pop("sa_column_kwargs", Undefined)
@@ -104,18 +105,23 @@ def __init__(self, default: Any = Undefined, **kwargs: Any) -> None:
104105
)
105106
if unique is not Undefined:
106107
raise RuntimeError(
107-
"Passing unique is not supported when " "also passing a sa_column"
108+
"Passing unique is not supported when also passing a sa_column"
108109
)
109110
if index is not Undefined:
110111
raise RuntimeError(
111-
"Passing index is not supported when " "also passing a sa_column"
112+
"Passing index is not supported when also passing a sa_column"
113+
)
114+
if sa_type is not Undefined:
115+
raise RuntimeError(
116+
"Passing sa_type is not supported when also passing a sa_column"
112117
)
113118
super().__init__(default=default, **kwargs)
114119
self.primary_key = primary_key
115120
self.nullable = nullable
116121
self.foreign_key = foreign_key
117122
self.unique = unique
118123
self.index = index
124+
self.sa_type = sa_type
119125
self.sa_column = sa_column
120126
self.sa_column_args = sa_column_args
121127
self.sa_column_kwargs = sa_column_kwargs
@@ -185,6 +191,7 @@ def Field(
185191
unique: Union[bool, UndefinedType] = Undefined,
186192
nullable: Union[bool, UndefinedType] = Undefined,
187193
index: Union[bool, UndefinedType] = Undefined,
194+
sa_type: Union[Type[Any], UndefinedType] = Undefined,
188195
sa_column_args: Union[Sequence[Any], UndefinedType] = Undefined,
189196
sa_column_kwargs: Union[Mapping[str, Any], UndefinedType] = Undefined,
190197
schema_extra: Optional[Dict[str, Any]] = None,
@@ -264,6 +271,7 @@ def Field(
264271
unique: Union[bool, UndefinedType] = Undefined,
265272
nullable: Union[bool, UndefinedType] = Undefined,
266273
index: Union[bool, UndefinedType] = Undefined,
274+
sa_type: Union[Type[Any], UndefinedType] = Undefined,
267275
sa_column: Union[Column, UndefinedType] = Undefined, # type: ignore
268276
sa_column_args: Union[Sequence[Any], UndefinedType] = Undefined,
269277
sa_column_kwargs: Union[Mapping[str, Any], UndefinedType] = Undefined,
@@ -300,6 +308,7 @@ def Field(
300308
unique=unique,
301309
nullable=nullable,
302310
index=index,
311+
sa_type=sa_type,
303312
sa_column=sa_column,
304313
sa_column_args=sa_column_args,
305314
sa_column_kwargs=sa_column_kwargs,
@@ -515,6 +524,9 @@ def __init__(
515524

516525

517526
def get_sqlalchemy_type(field: ModelField) -> Any:
527+
sa_type = getattr(field.field_info, "sa_type", Undefined) # noqa: B009
528+
if sa_type is not Undefined:
529+
return sa_type
518530
if isinstance(field.type_, type) and field.shape == SHAPE_SINGLETON:
519531
# Check enums first as an enum can also be a str, needed by Pydantic/FastAPI
520532
if issubclass(field.type_, Enum):

tests/test_field_sa_column.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ class Item(SQLModel, table=True):
3939
)
4040

4141

42+
def test_sa_column_no_type() -> None:
43+
with pytest.raises(RuntimeError):
44+
45+
class Item(SQLModel, table=True):
46+
id: Optional[int] = Field(
47+
default=None,
48+
sa_type=Integer,
49+
sa_column=Column(Integer, primary_key=True),
50+
)
51+
52+
4253
def test_sa_column_no_primary_key() -> None:
4354
with pytest.raises(RuntimeError):
4455

0 commit comments

Comments
 (0)