[Django] #33512: DateTimeField auto_add_now is NUL when creating child model object referencing parent model's object

6 views
Skip to first unread message

Django

unread,
Feb 11, 2022, 10:10:47 AM2/11/22
to django-...@googlegroups.com
#33512: DateTimeField auto_add_now is NUL when creating child model object
referencing parent model's object
-------------------------------------+-------------------------------------
Reporter: eeriks | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 4.0
layer (models, ORM) |
Severity: Normal | Keywords: auto_add_now
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Description:
When creating Model object which inherits from other Model which has
`DateTimeField` with `auto_add_now=True` and passing `parent_ptr_id`
value, then Django tries to update base Model by setting the auto_now_add
field to NUL (although the field is null=False)

Expected result:
ChildModel instance created with a link to ParentModel.

Code examples:
`models.py`
{{{
class ParentModel(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)


class ChildModel(SomeBaseModel):
some_field = models.CharField(max_length=12)
}}}

`manage.py shell`
{{{
from app.models import ParentModel, ChildModel
parent_object = ParentModel.objects.create() # OK
ChildModel.objects.create() # OK
ChildModel.objects.create(parentmodel_ptr=parent_object) # IntegrityError
}}}

Actual result:
Django tries to update ParentModel's `created` with NULL
{{{
UPDATE "app_parentmodel" SET "created" = NULL, "modified" = ? WHERE
"app_parentmodel"."id" = ?
}}}
And raises
{{{
django.db.utils.IntegrityError: NOT NULL constraint failed:
app_parentmodel.created
}}}


Noticed in Django 2.2, reproduced in 3.2 and 4.0. with both Postgres and
SQlite database backends.
[https://github.com/eeriks/django-demo Demo project on GitHub] to
reproduce bug.

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

Django

unread,
Feb 14, 2022, 1:39:32 AM2/14/22
to django-...@googlegroups.com
#33512: Creating a child instance with a parent containing
DateTimeField(auto_add_now=True) raises IntegrityError.
-------------------------------------+-------------------------------------
Reporter: Eriks K | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: auto_add_now | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

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


Old description:

New description:

Description:
When creating Model object which inherits from other Model which has
`DateTimeField` with `auto_add_now=True` and passing `parent_ptr_id`
value, then Django tries to update base Model by setting the auto_now_add
field to NUL (although the field is null=False)

Expected result:
ChildModel instance created with a link to ParentModel.

Code examples:
`models.py`
{{{
class ParentModel(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)


class ChildModel(ParentModel):
some_field = models.CharField(max_length=12)
}}}

`manage.py shell`
{{{
from app.models import ParentModel, ChildModel
parent_object = ParentModel.objects.create() # OK
ChildModel.objects.create() # OK
ChildModel.objects.create(parentmodel_ptr=parent_object) # IntegrityError
}}}

Actual result:
Django tries to update ParentModel's `created` with NULL
{{{
UPDATE "app_parentmodel" SET "created" = NULL, "modified" = ? WHERE
"app_parentmodel"."id" = ?
}}}
And raises
{{{
django.db.utils.IntegrityError: NOT NULL constraint failed:
app_parentmodel.created
}}}


Noticed in Django 2.2, reproduced in 3.2 and 4.0. with both Postgres and
SQlite database backends.
[https://github.com/eeriks/django-demo Demo project on GitHub] to
reproduce bug.

--

Comment:

Thanks for the report! It looks like a different scenario for the same
issue as described in #24539.

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

Reply all
Reply to author
Forward
0 new messages