#8892: ForeignKey relation not saved as expected
------------------------------+---------------------------------------------
 Reporter:  julien            |       Owner:  nobody    
   Status:  new               |   Milestone:            
Component:  Database wrapper  |     Version:  1.0       
 Keywords:                    |       Stage:  Unreviewed
Has_patch:  0                 |  
------------------------------+---------------------------------------------
 Here are two simple models:

 {{{
 class ModelA(models.Model):
     name = models.CharField(max_length=10)

 class ModelB(models.Model):
     name = models.CharField(max_length=10)
     a = models.ForeignKey(ModelA, blank=True, null=True)
 }}}

 Now, see the following sequence. When setting the relation *before* saving
 the related object, the relation itself is not properly saved:

 {{{
 >>> a = ModelA(name='foo')
 >>> b = ModelB(name='bar')
 >>> b.a = a # Set relation *before* saving related object
 >>> a.save() # Save related object
 >>> b.save()
 >>> b.a
 <ModelA: ModelA object>
 >>> b = ModelB.objects.get(name='bar')
 >>> b.a
 <NoneType: None>
 }}}

 Logically I'd expect that, since `a` is saved before `b`, the relation
 would be properly saved but it is not.

 Yet, if setting the relation *after* saving the related object, then it
 works fine (in the below example I only swapped 2 lines: `b.a = a` and
 `a.save()`):

 {{{
 >>> a = ModelA(name='foo')
 >>> b = ModelB(name='bar')
 >>> a.save() # Save related object
 >>> b.a = a # Set relation *after* saving related object
 >>> b.save()
 >>> b.a
 <ModelA: ModelA object>
 >>> b = ModelB.objects.get(name='bar')
 >>> b.a
 <ModelA: ModelA object>
 }}}

 I've tested with MySQL and SQLite.

-- 
Ticket URL: <http://code.djangoproject.com/ticket/8892>
Django Code <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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to