@@ -30,6 +30,7 @@ async def create_model(
30
30
self ,
31
31
session : AsyncSession ,
32
32
obj : CreateSchema ,
33
+ flush : bool = False ,
33
34
commit : bool = False ,
34
35
** kwargs ,
35
36
) -> Model :
@@ -38,6 +39,7 @@ async def create_model(
38
39
39
40
:param session: The SQLAlchemy async session.
40
41
:param obj: The Pydantic schema containing data to be saved.
42
+ :param flush: If `True`, flush all object changes to the database. Default is `False`.
41
43
:param commit: If `True`, commits the transaction immediately. Default is `False`.
42
44
:param kwargs: Additional model data not included in the pydantic schema.
43
45
:return:
@@ -47,6 +49,8 @@ async def create_model(
47
49
else :
48
50
ins = self .model (** obj .model_dump (), ** kwargs )
49
51
session .add (ins )
52
+ if flush :
53
+ await session .flush ()
50
54
if commit :
51
55
await session .commit ()
52
56
return ins
@@ -55,6 +59,7 @@ async def create_models(
55
59
self ,
56
60
session : AsyncSession ,
57
61
objs : Iterable [CreateSchema ],
62
+ flush : bool = False ,
58
63
commit : bool = False ,
59
64
** kwargs ,
60
65
) -> list [Model ]:
@@ -63,6 +68,7 @@ async def create_models(
63
68
64
69
:param session: The SQLAlchemy async session.
65
70
:param objs: The Pydantic schema list containing data to be saved.
71
+ :param flush: If `True`, flush all object changes to the database. Default is `False`.
66
72
:param commit: If `True`, commits the transaction immediately. Default is `False`.
67
73
:param kwargs: Additional model data not included in the pydantic schema.
68
74
:return:
@@ -75,6 +81,8 @@ async def create_models(
75
81
ins = self .model (** obj .model_dump (), ** kwargs )
76
82
ins_list .append (ins )
77
83
session .add_all (ins_list )
84
+ if flush :
85
+ await session .flush ()
78
86
if commit :
79
87
await session .commit ()
80
88
return ins_list
@@ -169,6 +177,7 @@ async def update_model(
169
177
session : AsyncSession ,
170
178
pk : int ,
171
179
obj : UpdateSchema | dict [str , Any ],
180
+ flush : bool = False ,
172
181
commit : bool = False ,
173
182
** kwargs ,
174
183
) -> int :
@@ -178,6 +187,7 @@ async def update_model(
178
187
:param session: The SQLAlchemy async session.
179
188
:param pk: The database primary key value.
180
189
:param obj: A pydantic schema or dictionary containing the update data
190
+ :param flush: If `True`, flush all object changes to the database. Default is `False`.
181
191
:param commit: If `True`, commits the transaction immediately. Default is `False`.
182
192
:return:
183
193
"""
@@ -189,6 +199,8 @@ async def update_model(
189
199
instance_data .update (kwargs )
190
200
stmt = update (self .model ).where (self .primary_key == pk ).values (** instance_data )
191
201
result = await session .execute (stmt )
202
+ if flush :
203
+ await session .flush ()
192
204
if commit :
193
205
await session .commit ()
194
206
return result .rowcount # type: ignore
@@ -198,6 +210,7 @@ async def update_model_by_column(
198
210
session : AsyncSession ,
199
211
obj : UpdateSchema | dict [str , Any ],
200
212
allow_multiple : bool = False ,
213
+ flush : bool = False ,
201
214
commit : bool = False ,
202
215
** kwargs ,
203
216
) -> int :
@@ -207,6 +220,7 @@ async def update_model_by_column(
207
220
:param session: The SQLAlchemy async session.
208
221
:param obj: A pydantic schema or dictionary containing the update data
209
222
:param allow_multiple: If `True`, allows updating multiple records that match the filters.
223
+ :param flush: If `True`, flush all object changes to the database. Default is `False`.
210
224
:param commit: If `True`, commits the transaction immediately. Default is `False`.
211
225
:param kwargs: Query expressions.
212
226
:return:
@@ -221,6 +235,8 @@ async def update_model_by_column(
221
235
instance_data = obj .model_dump (exclude_unset = True )
222
236
stmt = update (self .model ).where (* filters ).values (** instance_data ) # type: ignore
223
237
result = await session .execute (stmt )
238
+ if flush :
239
+ await session .flush ()
224
240
if commit :
225
241
await session .commit ()
226
242
return result .rowcount # type: ignore
@@ -229,18 +245,22 @@ async def delete_model(
229
245
self ,
230
246
session : AsyncSession ,
231
247
pk : int ,
248
+ flush : bool = False ,
232
249
commit : bool = False ,
233
250
) -> int :
234
251
"""
235
252
Delete an instance by model's primary key
236
253
237
254
:param session: The SQLAlchemy async session.
238
255
:param pk: The database primary key value.
256
+ :param flush: If `True`, flush all object changes to the database. Default is `False`.
239
257
:param commit: If `True`, commits the transaction immediately. Default is `False`.
240
258
:return:
241
259
"""
242
260
stmt = delete (self .model ).where (self .primary_key == pk )
243
261
result = await session .execute (stmt )
262
+ if flush :
263
+ await session .flush ()
244
264
if commit :
245
265
await session .commit ()
246
266
return result .rowcount # type: ignore
@@ -251,18 +271,20 @@ async def delete_model_by_column(
251
271
allow_multiple : bool = False ,
252
272
logical_deletion : bool = False ,
253
273
deleted_flag_column : str = 'del_flag' ,
274
+ flush : bool = False ,
254
275
commit : bool = False ,
255
276
** kwargs ,
256
277
) -> int :
257
278
"""
258
- Delete
279
+ Delete an instance by model column
259
280
260
281
:param session: The SQLAlchemy async session.
261
- :param commit: If `True`, commits the transaction immediately. Default is `False`.
262
- :param kwargs: Query expressions.
263
282
:param allow_multiple: If `True`, allows deleting multiple records that match the filters.
264
283
:param logical_deletion: If `True`, enable logical deletion instead of physical deletion
265
284
:param deleted_flag_column: Specify the flag column for logical deletion
285
+ :param flush: If `True`, flush all object changes to the database. Default is `False`.
286
+ :param commit: If `True`, commits the transaction immediately. Default is `False`.
287
+ :param kwargs: Query expressions.
266
288
:return:
267
289
"""
268
290
filters = parse_filters (self .model , ** kwargs )
@@ -275,6 +297,8 @@ async def delete_model_by_column(
275
297
else :
276
298
stmt = delete (self .model ).where (* filters )
277
299
result = await session .execute (stmt )
300
+ if flush :
301
+ await session .flush ()
278
302
if commit :
279
303
await session .commit ()
280
304
return result .rowcount # type: ignore
0 commit comments