#26703: field automatically choosen as primary_key (primary_key=False do not
work)
-------------------------------------+-------------------------------------
Reporter: nbremond | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: multi-table | Triage Stage:
inheritance, primary_key, | Unreviewed
OneToOneField |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by nbremond):
* status: closed => new
* resolution: duplicate =>
Comment:
I don't know if I have to reply here, on #26320 or on a new ticket, but
the solution given by charette on comment:2 does not really work.
The problem on fields is solved but I think there is still a bug during
objects instanciation.
{{{#!python
#models.py
from django.db import models
class Base(models.Model):
def __str__(self):#indicate if the object is a member of a subclass
try:
a=self.a
return "a A pk="+str(self.pk)+" (other="+str(a.other.pk)+")"
except A.DoesNotExist:
pass
try:
z=self.z
return "not a A but a Z pk="+str(self.pk)
except Z.DoesNotExist:
pass
return "not a A neither a Z pk="+str(self.pk)
class A(Base):
base_ptr = models.OneToOneField(Base, parent_link=True,
primary_key=True)
other = models.OneToOneField(Base, related_name='other_base') #I do
not have null=True
class Z(Base):
pass
}}}
I can manipulate the `Z` objects :
adding a new `Z` :
{{{#!python
>>> z=Z()
>>> z.save()
>>> Base.objects.all()
[<Base: not a A but a Z pk=1>]
}}}
if I try to do the same thing with a `A` (with a new database):
{{{#!python
>>> other=Base()
>>> other.save()
>>> a=A(other=other)
>>> a.save()
>>> a
<A: not a A neither a Z pk=None> #a is supposed to have a pk which is not
None
>>> a.other
<Base: a A pk=1 (other=1)>#other is supposed to be a base and not a A
>>> Base.objects.all()
[<Base: a A pk=1 (other=1)>]#I was expecting two objects
}}}
It seems to be possible to change a `Base` to a `A`:
{{{#!python
>>> other=Base()
>>> other.save()
>>> b=Base()
>>> b.save()
>>> Base.objects.all()
[<Base: not a A neither a Z pk=1>, <Base: not a A neither a Z pk=2>]
>>> a=A(other=other,base_ptr=b)
>>> a.save()
>>> a
<A: not a A neither a Z pk=2>#str(a) should return "a A" but this work if
we reload the object
>>> a.other
<Base: not a A neither a Z pk=1>
>>> Base.objects.all()
<Base: not a A neither a Z pk=1>, <Base: a A pk=2 (other=1)>]
}}}
This is maybe also deprecated but it can be a bug in the next version and
I didn't see a fix in [https://github.com/django/django/pull/6490 PR] (I
quickly look the pull request, maybe I'm wrong)
--
Ticket URL: <https://code.djangoproject.com/ticket/26703#comment:6>
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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.fd77e0da10ee95552258bebb3f697b83%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.