Skip to content

Commit 2fb499a

Browse files
committed
Add support for specifying column type.
Instead of having to set the whole column, we can just override the type of the field itself. This works much nicer with the rest of sqlmodel.
1 parent 02da85c commit 2fb499a

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

sqlmodel/main.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ipaddress
2+
from sqlalchemy.sql.type_api import TypeEngine
23
import uuid
34
import weakref
45
from datetime import date, datetime, time, timedelta
@@ -72,6 +73,7 @@ def __init__(self, default: Any = Undefined, **kwargs: Any) -> None:
7273
foreign_key = kwargs.pop("foreign_key", Undefined)
7374
index = kwargs.pop("index", Undefined)
7475
sa_column = kwargs.pop("sa_column", Undefined)
76+
sa_column_type = kwargs.pop("sa_column_type", Undefined)
7577
sa_column_args = kwargs.pop("sa_column_args", Undefined)
7678
sa_column_kwargs = kwargs.pop("sa_column_kwargs", Undefined)
7779
if sa_column is not Undefined:
@@ -91,6 +93,7 @@ def __init__(self, default: Any = Undefined, **kwargs: Any) -> None:
9193
self.foreign_key = foreign_key
9294
self.index = index
9395
self.sa_column = sa_column
96+
self.sa_column_type = sa_column_type
9497
self.sa_column_args = sa_column_args
9598
self.sa_column_kwargs = sa_column_kwargs
9699

@@ -153,6 +156,7 @@ def Field(
153156
nullable: Union[bool, UndefinedType] = Undefined,
154157
index: Union[bool, UndefinedType] = Undefined,
155158
sa_column: Union[Column, UndefinedType] = Undefined,
159+
sa_column_type: Union[TypeEngine, UndefinedType] = Undefined,
156160
sa_column_args: Union[Sequence[Any], UndefinedType] = Undefined,
157161
sa_column_kwargs: Union[Mapping[str, Any], UndefinedType] = Undefined,
158162
schema_extra: Optional[Dict[str, Any]] = None,
@@ -183,6 +187,7 @@ def Field(
183187
nullable=nullable,
184188
index=index,
185189
sa_column=sa_column,
190+
sa_column_type=sa_column_type,
186191
sa_column_args=sa_column_args,
187192
sa_column_kwargs=sa_column_kwargs,
188193
**current_schema_extra,
@@ -412,7 +417,11 @@ def get_column_from_field(field: ModelField) -> Column:
412417
sa_column = getattr(field.field_info, "sa_column", Undefined)
413418
if isinstance(sa_column, Column):
414419
return sa_column
415-
sa_type = get_sqlachemy_type(field)
420+
sa_column_type = getattr(field.field_info, "sa_column_type", Undefined)
421+
if sa_column_type is not Undefined:
422+
sa_type = sa_column_type
423+
else:
424+
sa_type = get_sqlachemy_type(field)
416425
primary_key = getattr(field.field_info, "primary_key", False)
417426
nullable = not field.required
418427
index = getattr(field.field_info, "index", Undefined)

0 commit comments

Comments
 (0)