[Django] #30531: Exception when creating an inherited model object with existing base

3 views
Skip to first unread message

Django

unread,
May 30, 2019, 10:24:15 AM5/30/19
to django-...@googlegroups.com
#30531: Exception when creating an inherited model object with existing base
-------------------------------------+-------------------------------------
Reporter: Victor | Owner: nobody
Porton |
Type: Bug | Status: new
Component: Database | Version: 2.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
The following intuitively right causes an exception:

{{{
class Base(models.Model):
v = models.IntegerField()

class Derived(Base):
base = models.OneToOneField(Base, on_delete=models.CASCADE,
primary_key=True)

class BugTestCase(TestCase):
def setUp(self):
self.base = Base.objects.create(v=0)

def test_bug(self):
Derived.objects.create(base=self.base)
}}}

Here it is:

{{{
django.db.utils.IntegrityError: NOT NULL constraint failed: mytest_base.v
}}}

It should work in the expected way not to raise an exception.

In the project I am developing now, I am going to write a workaround with
a raw SQL query :-(

I will attach full test source.

--
Ticket URL: <https://code.djangoproject.com/ticket/30531>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 30, 2019, 10:24:47 AM5/30/19
to django-...@googlegroups.com
#30531: Exception when creating an inherited model object with existing base
-------------------------------------+-------------------------------------
Reporter: Victor Porton | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.2
(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 Victor Porton):

* Attachment "django-create-inherited-bug.tar.gz" added.

Source which causes the bug

Django

unread,
May 30, 2019, 11:16:32 AM5/30/19
to django-...@googlegroups.com
#30531: Exception when creating an inherited model object with existing base
-------------------------------------+-------------------------------------
Reporter: Victor Porton | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Normal | Resolution: invalid
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 Simon Charette):

* status: new => closed
* resolution: => invalid


Comment:

If you want to use `Derived.base` as your inheritance parent link you have
to
[https://docs.djangoproject.com/en/2.2/ref/models/fields/#django.db.models.OneToOneField.parent_link
mark it accordingly].

Right now your table `Derived` model has two o2o fields pointing at
`Base`; the explicit one you defined and the implicit parent link
`base_ptr`. When you try to create your `Derived` object in `test_bug`
it's the `base_ptr.v` field value that is missing.

Adding `parent_link=True` to your `Derived.base` field should address your
issue.

Closing per TicketClosingReasons/UseSupportChannels.

--
Ticket URL: <https://code.djangoproject.com/ticket/30531#comment:1>

Reply all
Reply to author
Forward
0 new messages