Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django DateTimeField with auto_now_add asks for default

Tags:

django

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?

like image 462
Arthur Tarasov Avatar asked May 26 '19 03:05

Arthur Tarasov


People also ask

What is difference between Auto_now and Auto_now_add?

auto_now - updates the value of field to current time and date every time the Model. save() is called. auto_now_add - updates the value with the time and date of creation of record.

What is Auto_now_add true?

auto_now_add Automatically set the field to now when the object is first created. Useful for creation of timestamps. Note that the current date is always used; it's not just a default value that you can override.


1 Answers

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.

like image 196
Seetharam Avatar answered Nov 15 '22 22:11

Seetharam