@@ -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
517526def 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 ):
0 commit comments