Why does auto_now_add result in a default field value (presumably corresponds to when I generated the migration?), but auto_now doesn’t need one?
Looking at the data, both are set to now() when a record is initially saved.
When you generated the migration, weren’t you asked to provide a default value for created?
This is because, there might already be rows in DB so it needs something to populate that created because it should be “automatically added at creation time”.
The auto_now_add field is set when the instance of that model is first created, and isn’t supposed to be changed after that. That means that in the migration process, a value needs to be provided to set that field for rows already in the table. (There is no other way for this value to be set on existing rows.)
From the code for django.db.migrations.questioner.InteractiveMigrationQuestioner.ask_auto_now_add_addition:
choice = self._choice_input(
f"It is impossible to add the field '{field_name}' with "
f"'auto_now_add=True' to {model_name} without providing a "
f"default. This is because the database needs something to "
f"populate existing rows.\n",
[
"Provide a one-off default now which will be set on all "
"existing rows",
"Quit and manually define a default value in models.py.",
],
)
This indicates to me that this option will exist in the migration, regardless of the presence or absence of any existing data.
On the other hand, the auto_now clause will update the objects every time they’re saved, and the migration process will populate that field when the migration is run.
Now I remember I was asked to provide the default value when generating the migration, and gave timezone.now()…
I don’t think I really understood migrations in the context of existing data; your answers have prodded me to work through it properly. It was a lot simpler to me once I deleted my dev database and migrations, and then recreated an initial migration with ‘created’ and ‘updated’ in place from the beginning.