#32332: Saving parent object after setting on child leads to data loss for 
parents
with non-numeric primary key.
-------------------------------------+-------------------------------------
     Reporter:  Charlie DeTar        |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  3.1
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

 * cc: robinh00d, Jon Dufresne (added)


Comment:

 Thanks for this report. `product_id` is an empty string in
 
[https://github.com/django/django/blob/2d6179c819010f6a9d00835d5893c4593c0b85a0/django/db/models/base.py#L936-L939
 _prepare_related_fields_for_save()] that's why `pk` from a related object
 is not used. We could use `empty_values`:
 {{{
 diff --git a/django/db/models/base.py b/django/db/models/base.py
 index 822aad080d..8e7a8e3ae7 100644
 --- a/django/db/models/base.py
 +++ b/django/db/models/base.py
 @@ -933,7 +933,7 @@ class Model(metaclass=ModelBase):
                          "%s() prohibited to prevent data loss due to
 unsaved "
                          "related object '%s'." % (operation_name,
 field.name)
                      )
 -                elif getattr(self, field.attname) is None:
 +                elif getattr(self, field.attname) in field.empty_values:
                      # Use pk from related object if it has been saved
 after
                      # an assignment.
                      setattr(self, field.attname, obj.pk)
 }}}
 but I'm not sure.

 Related with #28147.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32332#comment:2>
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.ca80d177d09d675b2cc00b32de203777%40djangoproject.com.

Reply via email to