Skip to content

Commit ae78c12

Browse files
austincollinpenaAustinPena
andauthored
Answering "How to do bulk insert / update?" (#690)
* Answered "How to do bulk insert / update" * Fixed typo Co-authored-by: AustinPena <[email protected]>
1 parent 161cbb8 commit ae78c12

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

docs/how-to/faq.rst

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,28 @@ same time, using a :class:`~gino.loader.TupleLoader` with two sub-loaders -
236236
Be ware of the :class:`tuple` in ``.gino.load((...))``.
237237

238238

239-
How to do bulk insert / update?
240-
-------------------------------
241239

242-
TBD #314
240+
How to do bulk or batch insert / update?
241+
-----------------------------------------
242+
243+
For a simple example, take a model that has one field, "name." In your application you have a list of names you would like to add to the database:
244+
245+
::
246+
247+
new_names = ["Austin", "Ali", "Jeff", "Marissa"]
248+
249+
To quickly insert the names in one query, first construct a dict with the ``{"model_key": "value"}`` format.
250+
251+
::
252+
253+
new_names_dict = [dict(name=new_name) for new_name in new_names]
254+
>> [{'name': 'Austin'}, {'name': 'Ali'}, {'name': 'Jeff'}, {'name': 'Marissa'}]
255+
256+
Finally, run an insert statement on the model.
257+
258+
::
243259

260+
await User.insert().gino.all(new_names_dict)
244261

245262
How to print the executed SQL?
246263
------------------------------

0 commit comments

Comments
 (0)