#9982: Inconsistent behavior on model save depending on whether OneToOneField
is a
primary key
------------------------------------------------------+---------------------
Reporter: [email protected] | Owner: nobody
Status: new | Milestone: post-1.0
Component: Database layer (models, ORM) | Version: 1.0
Keywords: OneToOneField primary_key IntegrityError | Stage: Unreviewed
Has_patch: 0 |
------------------------------------------------------+---------------------
Hi,
When using a OneToOneField to make one model extend another, I noticed a
behavioral inconsistency depending on whether the related model's
OneToOneField was also the primary key or not.
The inconsistency is to do with when save() can be called on model
instances.
Consider the following example models:
{{{
class BaseModel(models.Model):
pass
class ExtendedModel(models.Model):
link = models.OneToOneField(BaseModel)
}}}
It is not possible to first instantiate these models and later on save
them - an IntegrityError would be raised when saving the ExtendedModel.
E.g.,
{{{
o1 = BaseModel()
o2 = ExtendedModel(link=o1)
o1.save()
o2.save()
}}}
when o2 is saved it will raise an IntegrityError because it doesn't have a
value for the primary key of the model it is related to. I think o1's
primary key is copied when o2 is initialized, and is None at this point
because o1 isn't saved yet.
However, if the definition of ExtendedModel is changed so that the
OneToOneField is also the primary key, it now becomes possible to save the
model instances in this order. To test this I changed the model definition
as follows:
{{{
class ExtendedModel(models.Model):
link = models.OneToOneField(BaseModel, primary_key=True)
}}}
When using this model definition it seems that o1's primary key is copied
when o2 is saved, as opposed to when o2 is initialized. This inconsistency
caused a lot of head scratching here.
Thanks!
Sean
--
Ticket URL: <http://code.djangoproject.com/ticket/9982>
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 [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
-~----------~----~----~----~------~----~------~--~---