[Django] #35037: DateTimeField with auto_now_add and default is inconsistent/broken

21 views
Skip to first unread message

Django

unread,
Dec 14, 2023, 8:24:50 AM12/14/23
to django-...@googlegroups.com
#35037: DateTimeField with auto_now_add and default is inconsistent/broken
-------------------------------------+-------------------------------------
Reporter: Norbert | Owner: nobody
Preining |
Type: Bug | Status: new
Component: Core | Version: 5.0
(Management commands) |
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 |
-------------------------------------+-------------------------------------
I wanted to update a model by adding a field
{{{
created_at = models.DateTimeField(auto_now_add=True)
}}}


But when I wanted to create migrations I got:
{{{
$ python manage.py makemigrations users
It is impossible to add the field 'created_at' with 'auto_now_add=True' to
customuser without providing a default. This is because the database needs
something to populate existing rows.
1) Provide a one-off default now which will be set on all existing rows
2) Quit and manually define a default value in models.py.
Select an option: 2
}}}

Ok, according to this, I added a default as in
{{{
created_at = models.DateTimeField(auto_now_add=True, default=datetime.now)
}}}

but creating migrations still complain:
{{{
$ python manage.py makemigrations users
SystemCheckError: System check identified some issues:

ERRORS:
users.CustomUser.created_at: (fields.E160) The options auto_now,
auto_now_add, and default are mutually exclusive. Only one of these
options may be present.
}}}

That means, according to the message from manage.py:
- `auto_now`, `auto_now_add`, and `default` are mutually exclusive
- but at the same time, I need to add a default for `auto_now_add` to be
added

I am puzzled

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

Django

unread,
Dec 14, 2023, 8:44:02 AM12/14/23
to django-...@googlegroups.com
#35037: DateTimeField with auto_now_add and default is inconsistent/broken
-------------------------------------+-------------------------------------
Reporter: Norbert Preining | Owner: nobody
Type: Bug | Status: new
Component: Core (Management | Version: 5.0
commands) |
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
-------------------------------------+-------------------------------------

Comment (by Norbert Preining):

What works is providing a one-off value by selection 1 in the first case.

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

Django

unread,
Dec 14, 2023, 8:45:00 AM12/14/23
to django-...@googlegroups.com
#35037: DateTimeField with auto_now_add and default is inconsistent/broken
-------------------------------------+-------------------------------------
Reporter: norbusan | Owner: nobody
Type: Bug | Status: closed

Component: Core (Management | Version: 5.0
commands) |
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 norbusan):

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


Comment:

Closing with invalid

--
Ticket URL: <https://code.djangoproject.com/ticket/35037#comment:2>

Reply all
Reply to author
Forward
0 new messages