#35037 closed Bug (invalid)

DateTimeField with auto_now_add and default is inconsistent/broken

Reported by: Norbert Preining Owned by: nobody
Component: Core (Management commands) Version: 5.0
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

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

Change History (2)

comment:1 by Norbert Preining, 11 months ago

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

comment:2 by Norbert Preining, 11 months ago

Resolution: invalid
Status: newclosed

Closing with invalid

Note: See TracTickets for help on using tickets.
Back to Top