29

I have this field in my model created_at = models.DateTimeField( auto_now_add = True )

When I try to make migrations I get an error:

You are trying to add the field 'created_at' with 'auto_now_add=True' to user wi
thout a default; the database needs something to populate existing rows.

 1) Provide a one-off default now (will be set on all existing rows)
 2) Quit, and let me add a default in models.py

I tried to set the default value but it says default and auto_now_add are mutually exclusive. Of course I could just use default without auto_now_add but I want to know why this error pops up. Did I miss something?

4
  • 1
    This is because you have already some rows without created_at value. Commented May 26, 2019 at 3:03
  • @shafik yes indeed. So what is the correct solution if I want to use auto_now_add? Commented May 26, 2019 at 3:09
  • 4
    You may choose the 1st option when running migrations then give the value timezone.now(). Then existing rows are populated with the current timestamp. Commented May 26, 2019 at 3:12
  • @shafik that did it. If you want to sum it up as an answer below, I will accept it the proper way Commented May 26, 2019 at 3:16

6 Answers 6

39

You have already some rows without created_at value. But when you added following field and run migration

created_at = models.DateTimeField( auto_now_add = True)

It expects the existing row has created_at value. So, It warns you at the time of migrations.

To solve this problem, you can choose the 1st option which says:
Provide a one-off default now (will be set on all existing rows).

By choosing the 1st option you are asking for value with default to timezone.now

Select an option: 1
Please enter the default value as valid Python.
Accept the default 'timezone.now' by pressing 'Enter' or provide another value.
The datetime and django.utils.timezone modules are available, so it is possible to provide e.g. timezone.now as a value.
Type 'exit' to exit this prompt
[default: timezone.now] >>> 

Press Enter and it will populate existing rows with the current timestamp.

4
  • 1
    Only the first option worked for me. Changes to the model gave me this error: The options auto_now, auto_now_add, and default are mutually exclusive. Only one of these options may be present. Commented May 26, 2019 at 3:45
  • Oh Ok. I forgot to mention this. It's better to follow the 1st option. Commented May 26, 2019 at 3:46
  • Django documents state that using auto_add_now=True will also set Blank=True, so why does it need to set values on rows that existed previously when Blank=True? Commented Nov 10, 2022 at 19:12
  • 1
    @MattWarren blank=True matters only for form validation, not validation when saving models. Commented Nov 18, 2022 at 18:47
9

Delete the migration file in app and you can makemigrtion process at the time no error raises, It raises due to the reason of previously created time object.

1

if you are using a sqlite database you can omit it and again make it, or you can write null=True as an argument for your DateTimeFeild() i suggest you the second one that i have done before

1

you can create two migrations,

  • the first one sets a default value,
  • the second remove the default and add the auto_now_add
0

That is because auto_now_add = True make django determine every instance of the model should have a value instead of null.

0

when migrating use:

  1. python manage.py makemigrations
  2. python manage.py migrate --run-syncdb

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.