{{{
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.
* Attachment "django-create-inherited-bug.tar.gz" added.
Source which causes the bug
* 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>