@@ -74,6 +74,7 @@ def __init__(self, default: Any = Undefined, **kwargs: Any) -> None:
74
74
foreign_key = kwargs .pop ("foreign_key" , Undefined )
75
75
unique = kwargs .pop ("unique" , False )
76
76
index = kwargs .pop ("index" , Undefined )
77
+ sa_type = kwargs .pop ("sa_type" , Undefined )
77
78
sa_column = kwargs .pop ("sa_column" , Undefined )
78
79
sa_column_args = kwargs .pop ("sa_column_args" , Undefined )
79
80
sa_column_kwargs = kwargs .pop ("sa_column_kwargs" , Undefined )
@@ -104,18 +105,23 @@ def __init__(self, default: Any = Undefined, **kwargs: Any) -> None:
104
105
)
105
106
if unique is not Undefined :
106
107
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"
108
109
)
109
110
if index is not Undefined :
110
111
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"
112
117
)
113
118
super ().__init__ (default = default , ** kwargs )
114
119
self .primary_key = primary_key
115
120
self .nullable = nullable
116
121
self .foreign_key = foreign_key
117
122
self .unique = unique
118
123
self .index = index
124
+ self .sa_type = sa_type
119
125
self .sa_column = sa_column
120
126
self .sa_column_args = sa_column_args
121
127
self .sa_column_kwargs = sa_column_kwargs
@@ -185,6 +191,7 @@ def Field(
185
191
unique : Union [bool , UndefinedType ] = Undefined ,
186
192
nullable : Union [bool , UndefinedType ] = Undefined ,
187
193
index : Union [bool , UndefinedType ] = Undefined ,
194
+ sa_type : Union [Type [Any ], UndefinedType ] = Undefined ,
188
195
sa_column_args : Union [Sequence [Any ], UndefinedType ] = Undefined ,
189
196
sa_column_kwargs : Union [Mapping [str , Any ], UndefinedType ] = Undefined ,
190
197
schema_extra : Optional [Dict [str , Any ]] = None ,
@@ -264,6 +271,7 @@ def Field(
264
271
unique : Union [bool , UndefinedType ] = Undefined ,
265
272
nullable : Union [bool , UndefinedType ] = Undefined ,
266
273
index : Union [bool , UndefinedType ] = Undefined ,
274
+ sa_type : Union [Type [Any ], UndefinedType ] = Undefined ,
267
275
sa_column : Union [Column , UndefinedType ] = Undefined , # type: ignore
268
276
sa_column_args : Union [Sequence [Any ], UndefinedType ] = Undefined ,
269
277
sa_column_kwargs : Union [Mapping [str , Any ], UndefinedType ] = Undefined ,
@@ -300,6 +308,7 @@ def Field(
300
308
unique = unique ,
301
309
nullable = nullable ,
302
310
index = index ,
311
+ sa_type = sa_type ,
303
312
sa_column = sa_column ,
304
313
sa_column_args = sa_column_args ,
305
314
sa_column_kwargs = sa_column_kwargs ,
@@ -515,6 +524,9 @@ def __init__(
515
524
516
525
517
526
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
518
530
if isinstance (field .type_ , type ) and field .shape == SHAPE_SINGLETON :
519
531
# Check enums first as an enum can also be a str, needed by Pydantic/FastAPI
520
532
if issubclass (field .type_ , Enum ):
0 commit comments