You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In #206, bulk creation of related objects was introduced. However, for _quantity=N, it will cause N calls to save() of the related class, causing N database queries. This comment claimed this was necessary to retrieve the IDs of the related objects.
According to the current django documentation, bulk_create will update the primary key of the objects if the field type is AutoField and the database used is PostgreSQL, MariaDB 10.5+ or SQLite 3.35+. I think this covers the majority of use cases. It would be nice if users could profit from the performance of bulk_create in these cases.
For implementing, django's can_return_rows_from_bulk_insert could be used to test whether this would be supported.
I think the calls are supposed to be baker.make calls instead of baker.prepare(?)
If this works (it does for me), doesn't this falsify the claim that the save() calls are needed to retrieve the IDs? Note that it does not specify any requirements on the database or django version used. Edit: I just saw it iterates all objects, not just the ones that were created.
The text was updated successfully, but these errors were encountered:
In some places, we can not use _bulk_create because baker doesn't create
M2M-entries then.
Tests are about 5 seconds (8%) faster with a hotpatched baker regarding
model-bakers/model_bakery#297
In #206, bulk creation of related objects was introduced. However, for
_quantity=N
, it will cause N calls tosave()
of the related class, causing N database queries. This comment claimed this was necessary to retrieve the IDs of the related objects.According to the current django documentation,
bulk_create
will update the primary key of the objects if the field type isAutoField
and the database used is PostgreSQL, MariaDB 10.5+ or SQLite 3.35+. I think this covers the majority of use cases. It would be nice if users could profit from the performance of bulk_create in these cases.For implementing, django's
can_return_rows_from_bulk_insert
could be used to test whether this would be supported.Expected behavior
with
I'd expect
to execute O(1) instead of O(N) database queries.
Actual behavior
model_bakery/tests/test_baker.py
Lines 386 to 391 in 850aadc
asserts that 6 = N+1 database queries happen.
On a side note: The documentation gives this workaround:
baker.make
calls instead ofbaker.prepare
(?)If this works (it does for me), doesn't this falsify the claim that theEdit: I just saw it iterates all objects, not just the ones that were created.save()
calls are needed to retrieve the IDs? Note that it does not specify any requirements on the database or django version used.The text was updated successfully, but these errors were encountered: