#14043: Incorrect and/or confusing behaviour with nullable OneToOneField
-------------------------------------------------------+--------------------
 Reporter:  theevilgeek                                |       Owner:  nobody   
 
   Status:  new                                        |   Milestone:           
 
Component:  Database layer (models, ORM)               |     Version:  SVN      
 
 Keywords:  OneToOneField, cascading delete, nullable  |       Stage:  
Unreviewed
Has_patch:  0                                          |  
-------------------------------------------------------+--------------------
 Attempting to "null" out a nullable OneToOneField before deleting the
 related object fails to prevent a cascading delete (i.e., both objects are
 still deleted whereas it seems only the related object ought to be
 deleted).

 Example code:

 {{{

 # Note: using Django trunk

 ###### MODELS ######

 class Person(models.Model):
   age = models.PositiveIntegerField()

   def die(self):
     self.soul.become_ghost()
     self.delete()


 class Soul(models.Model):
   person = models.OneToOneField(Person, null=True)
   is_alive = models.BooleanField(default=True)

   def become_ghost(self):
     self.person = None
     self.is_alive = False
     self.save()


 ###### TESTCASE (INTERACTIVE) ######

 # Type a few commands in "python manage.py shell"

 >>> from app.models import Person, Soul
 >>>
 >>> bob = Person.objects.create(age=34)
 >>> bobs_soul = Soul.objects.create(person=bob)

 # Let's see what's happening in MySQL (switching programs...)

 mysql> select * from app_person;
 +----+-----+
 | id | age |
 +----+-----+
 |  2 |  34 |
 +----+-----+
 1 row in set (0.00 sec)

 mysql> select * from app_soul;
 +----+-----------+----------+
 | id | person_id | is_alive |
 +----+-----------+----------+
 |  2 |         2 |        1 |
 +----+-----------+----------+
 1 row in set (0.00 sec)

 # Okay, that looks good; let's kill him (switching programs again...)

 >>> bob.die()

 # Back to MySQL

 mysql> select * from app_person;
 Empty set (0.00 sec)

 mysql> select * from app_soul;
 Empty set (0.00 sec)

 ### Huh!??! Why is app_soul being deleted?

 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/14043>
Django <http://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 post to this group, send email to django-upda...@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to