For MySQL InnoDB tables the behavior is described here:
http://dev.mysql.com/doc/refman/5.1/en/innodb-auto-increment-handling.html,
see "Gaps in auto-increment values for “bulk inserts”" item. So, you
are safe as long as the lock mode is correct, and assuming the lock
mode is the default is OK for Django.

For SQLite there can't be concurrent inserts (tables are locked
exclusively when writing), so you can just check the max id from the
table after insert and calculate the values from that.

There are still all the 3rd party databases (mssql for example), so
even if all core backends can support the multi-table bulk create,
you'll need to take in account that this isn't necessarily the case
for all possible backends.

Personally I think we should just rename the method to fast_create()
and allow it to fall back to single row at time behavior. This way you
could safely call it for any objects on any database and know the
results will be OK. I don't see much point in throwing errors when
batch insert isn't possible. If batch insert isn't possible, the user
needs to insert the values one row at a time in any case.

 - Anssi

On Tue, Mar 29, 2016 at 11:47 AM, Podrigal, Aron
<ar...@guaranteedplus.com> wrote:
> Hi,
>
> I worked on supporting bulk_insert for multi table inheritance here [1] I
> would like to get some feedback.
>
> bulk_create can be used in one of 2 ways
>
> 1) If you already have parent records in the database and want to
> bulk_insert into the child table only.
> 2) There isn't any parent records, and need to create them as well.
>
> In the first case, all database backends can support it. It simply requires
> the user to set the `parent_ptr` attributes for all child instances, and
> then do  bulk_create.
> In the second case it gets tricky and it cannot be supported for all
> databases for any model as it requires a way to get all the ids from the
> inserted parent records. Postgres is the most flexible in that case and
> supports bulk_insert for any table and any type of field because it supports
> the RETURNING clause so we can always retrieve the ids for the inserted
> rows. For sqlite we can only support bulk_create if the model does not have
> a parent with an AutoField.
> for MySQL I think we can rely on `LAST_INSERT_ID()` which will return the
> first ID out of the rows count inserted, so we can than generate a list of
> IDs of `range(last_insert_id, last_insert_id + count_rows)`.
>
> Can anyone confirm if we can rely on MySQL last_insert_id to be consistent
> and and without gaps for all records inserted at once?
>
> Thanks.
>
> [1]
> https://github.com/django/django/compare/master...ar45:allow_bulk_insert_multi_inheritance
>
>
> --
> Aron Podrigal
> -
> '1000001', '1110010', '1101111', '1101110'   '1010000', '1101111',
> '1100100', '1110010', '1101001', '1100111', '1100001', '1101100'
>
> P: '2b', '31', '33', '34', '37', '34', '35', '38', '36', '30', '39', '39'
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django developers (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-developers+unsubscr...@googlegroups.com.
> To post to this group, send email to django-developers@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-developers.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-developers/CANJp-yh7SXmC1a3T3RZjdDApdAqo6Op06DsQCqOQ0LhbmVhY5g%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/CALMtK1EfyCqE37hbdB%3DvyTvCRmJ6zqspSfng1-kxn1Evwtr_Lg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to