Code

Opened 2 years ago

Last modified 15 months ago

#18305 new Bug

force_update/force_insert not passed up the inheritance chain in .save()

Reported by: akaariai Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When saving a model, the force_update and force_insert are not passed up to parent model saves in model.save(). For example:

class Place(models.Model):
    name = TextField()

class Restaurant(Place):
    serves_pizza = models.BooleanField()

r = Restaurant.objects.create(name='Pizzeria pizza-kebab', serves_pizza=True)
r.serves_pizza = False
r.save(force_update=True)

Now, the r is updated correctly. Then, when saving the Place, the force_update isn't passed to the save_base, and thus the Place is first queried - so, the force_update isn't honored.

The same is true for force_insert. However, in this case it is possible that the force_insert is meant only for the Restaurant. You can currently do this:

p = Place.objects.create(name='p')
r = Restaurant(place=p, serves_pizza=False)
r.save(force_insert=True)

This would however be impossible if the force_insert would be passed up the inheritance chain.

Fixing the force_update case seems clear - however the force_insert case needs at least some consideration for backwards compatibility reasons.

Attachments (0)

Change History (2)

comment:1 Changed 2 years ago by akaariai

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

Patches for force_update and force_insert fixes available at: https://github.com/akaariai/django/tree/ticket_18305 (just force_update) and https://github.com/akaariai/django/tree/ticket_18305_both (contains also force_insert).

I am marking this DDN, as I am not sure if fixing the force_insert can be considered backwards compatible. If there were a way to save only the child model (the restaurant), then this would be easier to accept, as we could add "do this instead:" to the release notes.

comment:2 Changed 15 months ago by Alex

  • Triage Stage changed from Design decision needed to Accepted

Marking as accpted, at least 50% of this is an obvious bug ;)

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as new
The owner will be changed from nobody to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.