#30382: force_insert not respected when saving parents
-------------------------------------+-------------------------------------
               Reporter:  Phill      |          Owner:  nobody
  Tornroth                           |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  2.2
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 We're using non-abstract model inheritance (table per model class) and
 issuing our own primary keys. When saving we pass force_insert=True to
 prevent the extra UPDATE statement that precedes the INSERT. The
 force_insert flag is respected on the child table but not on the parent.
 So given:

 {{{
 class ParentModel(models.Model):
     id = models.BigIntegerField(primary_key=True)

 class ChildModel(ParentModel):
     pass

 ChildModel(id=1).save(force_insert=True)
 }}}

 We'll see queries:

 1. UPDATE app_parentmodel  (no rows affected)
 2. INSERT app_parentmodel
 3. INSERT app_childmodel

 This is because Model.save_base doesn't pass force_insert along to
 Model._save_parents, and onto Model._save_table. Doing so would prevent
 the extra UPDATE and respect the spirit of the force_insert feature. This
 is a change I've made locally and seems to work / is pretty
 straightforward. I'm curious though if there's intent behind not carrying
 the force_insert functionality through for parents. I couldn't find any
 discussion on the topic.

 For context about why this is particularly problematic in our case --
 we're using MySQL w/ Innodb and Innodb will take out different exclusive
 locks for the UPDATE and INSERT in question -- so if you're creating new
 ChildModel instances in parallel you can get deadlocks when multiple
 threads issue query #1 and then need a lock with insert intention in order
 to get #2.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30382>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/057.9d6619fbc3dea5e37fdf639004dcced0%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to