From e0a57feebe87404167dfcb439f8f468fc9cf618c Mon Sep 17 00:00:00 2001 From: FAQ Bot Date: Mon, 7 Sep 2026 07:34:27 +0000 Subject: [PATCH 1/2] =?UTF-8?q?NEW:=20`makemigrations`=20fails=20with=20?= =?UTF-8?q?=E2=80=9Cimpossible=20to=20add=20the=20field=20...=20auto=5Fnow?= =?UTF-8?q?=5Fad?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ons-auto-now-add-impossible-add-field-f.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 _questions/ai-dev-tools-zoomcamp/module-4/006_0ffccc6d06_makemigrations-auto-now-add-impossible-add-field-f.md diff --git a/_questions/ai-dev-tools-zoomcamp/module-4/006_0ffccc6d06_makemigrations-auto-now-add-impossible-add-field-f.md b/_questions/ai-dev-tools-zoomcamp/module-4/006_0ffccc6d06_makemigrations-auto-now-add-impossible-add-field-f.md new file mode 100644 index 00000000..999e862c --- /dev/null +++ b/_questions/ai-dev-tools-zoomcamp/module-4/006_0ffccc6d06_makemigrations-auto-now-add-impossible-add-field-f.md @@ -0,0 +1,36 @@ +--- +id: 0ffccc6d06 +question: '`makemigrations` fails with “impossible to add the field ... auto_now_add=True” + — how do I fix it?' +sort_order: 6 +--- + +This error happens when you add `created_at = models.DateTimeField(auto_now_add=True)` to a Django model that already has rows in the database. Django can’t invent timestamps for the existing rows, so it stops and asks for a default. + +Three common fixes: + +1) Answer the interactive prompt +Run `uv run python manage.py makemigrations` yourself and choose the option that sets the default (often Django suggests `timezone.now`). + +2) Recreate the database (if it’s disposable) +For early homework/dev scenarios where the DB can be thrown away: + +```bash +rm -f db.sqlite3 +rm -f chores/migrations/0*.py # only if those migrations are also throwaway +uv run python manage.py makemigrations +uv run python manage.py migrate +``` + +3) Avoid `auto_now_add` and use a default instead +If you want a timestamp without hitting the prompt, use a default value: + +```python +from django.utils import timezone + +created_at = models.DateTimeField(default=timezone.now) +``` + +Note: `auto_now_add=True` sets the value only on insert and is effectively read-only on updates. If you need to override timestamps in a seed script or tests, prefer `default=timezone.now`. + +Tip for AI agents: if your agent evolves the models multiple times in one session, run `makemigrations` after each meaningful model change—small migrations are easier to interpret and revert. \ No newline at end of file From 4eb9ebd291f47b037834d7597811e3d3cdc46d49 Mon Sep 17 00:00:00 2001 From: Alexey Grigorev Date: Wed, 9 Sep 2026 15:59:14 +0200 Subject: [PATCH 2/2] Repoint auto_now_add FAQ to Unit 1 homework, restore EOFError note (#381) --- ...igrations-auto-now-add-impossible-add-field-f.md} | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) rename _questions/ai-dev-tools-zoomcamp/{module-4/006_0ffccc6d06_makemigrations-auto-now-add-impossible-add-field-f.md => 01-ai-native-workflow-homework/001_0ffccc6d06_makemigrations-auto-now-add-impossible-add-field-f.md} (74%) diff --git a/_questions/ai-dev-tools-zoomcamp/module-4/006_0ffccc6d06_makemigrations-auto-now-add-impossible-add-field-f.md b/_questions/ai-dev-tools-zoomcamp/01-ai-native-workflow-homework/001_0ffccc6d06_makemigrations-auto-now-add-impossible-add-field-f.md similarity index 74% rename from _questions/ai-dev-tools-zoomcamp/module-4/006_0ffccc6d06_makemigrations-auto-now-add-impossible-add-field-f.md rename to _questions/ai-dev-tools-zoomcamp/01-ai-native-workflow-homework/001_0ffccc6d06_makemigrations-auto-now-add-impossible-add-field-f.md index 999e862c..ce47057a 100644 --- a/_questions/ai-dev-tools-zoomcamp/module-4/006_0ffccc6d06_makemigrations-auto-now-add-impossible-add-field-f.md +++ b/_questions/ai-dev-tools-zoomcamp/01-ai-native-workflow-homework/001_0ffccc6d06_makemigrations-auto-now-add-impossible-add-field-f.md @@ -2,18 +2,20 @@ id: 0ffccc6d06 question: '`makemigrations` fails with “impossible to add the field ... auto_now_add=True” — how do I fix it?' -sort_order: 6 +sort_order: 1 --- -This error happens when you add `created_at = models.DateTimeField(auto_now_add=True)` to a Django model that already has rows in the database. Django can’t invent timestamps for the existing rows, so it stops and asks for a default. +This error happens when you add `created_at = models.DateTimeField(auto_now_add=True)` to a Django model that already has rows in the database. Django can't invent timestamps for the existing rows, so it stops and asks for a default. + +It's easy to miss because the prompt is interactive. If your coding agent runs `makemigrations` non-interactively, it crashes with `EOFError: EOF when reading a line` instead of showing the question — which looks like an unrelated bug. Three common fixes: 1) Answer the interactive prompt Run `uv run python manage.py makemigrations` yourself and choose the option that sets the default (often Django suggests `timezone.now`). -2) Recreate the database (if it’s disposable) -For early homework/dev scenarios where the DB can be thrown away: +2) Recreate the database (if it's disposable) +For early homework/dev scenarios where the DB can be thrown away (the sqlite file should be in `.gitignore` anyway): ```bash rm -f db.sqlite3 @@ -33,4 +35,4 @@ created_at = models.DateTimeField(default=timezone.now) Note: `auto_now_add=True` sets the value only on insert and is effectively read-only on updates. If you need to override timestamps in a seed script or tests, prefer `default=timezone.now`. -Tip for AI agents: if your agent evolves the models multiple times in one session, run `makemigrations` after each meaningful model change—small migrations are easier to interpret and revert. \ No newline at end of file +Tip for AI agents: if your agent evolves the models multiple times in one session, run `makemigrations` after each meaningful model change—small migrations are easier to interpret and revert.