Opened 6 years ago
Closed 6 years ago
#30531 closed Bug (invalid)
Exception when creating an inherited model object with existing base
Reported by: | Victor Porton | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 2.2 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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.
Attachments (1)
Change History (2)
by , 6 years ago
Attachment: | django-create-inherited-bug.tar.gz added |
---|
comment:1 by , 6 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
If you want to use Derived.base
as your inheritance parent link you have to 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.
Source which causes the bug