[Django] #22512: Adding auto_now field to model causes migrate exception

18 views
Skip to first unread message

Django

unread,
Apr 25, 2014, 12:50:52 AM4/25/14
to django-...@googlegroups.com
#22512: Adding auto_now field to model causes migrate exception
---------------------------------+------------------------
Reporter: jaddison | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.7-beta-1
Severity: Release blocker | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------
I have a feeling this bug has already been filed, but I couldn't (easily)
find it.

It looks like the makemigrations command is failing to import (or use) the
datetime module properly. My steps:

* On an existing model, add a DateTimeField with `auto_now=True` set
* Run `makemigrations` command
* Run `migrate` command

Result is an exception, detailed below.

Calling makemigrations:

{{{
(project1)jaddison@jamess-air-2:~/projects/project1 (master)$ ./manage.py
makemigrations
You are trying to add a non-nullable field 'updated' to contentlink
without a default;
we can't do that (the database needs something to populate existing rows).
Please select a fix:
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
Select an option: 1
Please enter the default value now, as valid Python
The datetime module is available, so you can do e.g. datetime.date.today()
>>> datetime.datetime.now()
Migrations for 'content':
0003_contentlink_updated.py:
- Add field updated to contentlink
}}}

Calling migrate:

{{{
(project1)jaddison@jamess-air-2:~/projects/project1 (master)$ ./manage.py
migrate
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/jaddison/.virtualenvs/project1/lib/python2.7/site-
packages/django/core/management/__init__.py", line 427, in
execute_from_command_line
utility.execute()
File "/Users/jaddison/.virtualenvs/project1/lib/python2.7/site-
packages/django/core/management/__init__.py", line 419, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/jaddison/.virtualenvs/project1/lib/python2.7/site-
packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Users/jaddison/.virtualenvs/project1/lib/python2.7/site-
packages/django/core/management/base.py", line 337, in execute
output = self.handle(*args, **options)
File "/Users/jaddison/.virtualenvs/project1/lib/python2.7/site-
packages/django/core/management/commands/migrate.py", line 62, in handle
executor = MigrationExecutor(connection,
self.migration_progress_callback)
File "/Users/jaddison/.virtualenvs/project1/lib/python2.7/site-
packages/django/db/migrations/executor.py", line 14, in __init__
self.loader = MigrationLoader(self.connection)
File "/Users/jaddison/.virtualenvs/project1/lib/python2.7/site-
packages/django/db/migrations/loader.py", line 48, in __init__
self.build_graph()
File "/Users/jaddison/.virtualenvs/project1/lib/python2.7/site-
packages/django/db/migrations/loader.py", line 145, in build_graph
self.load_disk()
File "/Users/jaddison/.virtualenvs/project1/lib/python2.7/site-
packages/django/db/migrations/loader.py", line 103, in load_disk
migration_module = import_module("%s.%s" % (module_name,
migration_name))
File
"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py",
line 37, in import_module
__import__(name)
File
"/Users/jaddison/projects/project1/content/migrations/0003_contentlink_updated.py",
line 6, in <module>
class Migration(migrations.Migration):
File
"/Users/jaddison/projects/project1/content/migrations/0003_contentlink_updated.py",
line 16, in Migration
field=models.DateTimeField(default=datetime(2014, 4, 25, 4, 32, 56,
89929), auto_now=True),
TypeError: 'module' object is not callable
}}}

The contents of the generated migration `0003_contentlink_updated.py`:

{{{
# encoding: utf8
from django.db import models, migrations
import datetime


class Migration(migrations.Migration):

dependencies = [
('content', '0002_contentlink_body'),
]

operations = [
migrations.AddField(
model_name='contentlink',
name='updated',
field=models.DateTimeField(default=datetime(2014, 4, 25, 4,
32, 56, 89929), auto_now=True),
preserve_default=False,
),
]
}}}

Note that it should be `default=datetime.datetime(2014, 4, 25, 4, 32, 56,
89929)` instead.

--
Ticket URL: <https://code.djangoproject.com/ticket/22512>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 25, 2014, 12:56:30 AM4/25/14
to django-...@googlegroups.com
#22512: Adding auto_now field to model causes migrate exception
---------------------------------+--------------------------------------

Reporter: jaddison | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.7-beta-1
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Changes (by jaddison):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

As an additional note, would it not be wise to use `timezone.now()` if
time zone support is active? (see
https://docs.djangoproject.com/en/1.7/topics/i18n/timezones/#naive-and-
aware-datetime-objects)

After fixing the migration to use `datetime.datetime(...)` in the original
bug report, the migration runs fine, however I get this warning:

{{{
...
Applying
content.0003_contentlink_updated.../Users/jaddison/.virtualenvs/project1/lib/python2.7
/site-packages/django/db/backends/sqlite3/base.py:63: RuntimeWarning:
SQLite received a naive datetime (2014-04-25 04:32:56.089929) while time
zone support is active.
RuntimeWarning)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/22512#comment:1>

Django

unread,
Apr 25, 2014, 1:06:11 AM4/25/14
to django-...@googlegroups.com
#22512: Adding auto_now field to model causes migrate exception
---------------------------------+--------------------------------------

Reporter: jaddison | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.7-beta-1
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------

Comment (by loic84):

I believe this was fixed with 074d3183d92cd5ed5da8f51e7048b12a96f55e0a,
could you try the master branch?

--
Ticket URL: <https://code.djangoproject.com/ticket/22512#comment:2>

Django

unread,
Apr 28, 2014, 8:01:14 AM4/28/14
to django-...@googlegroups.com
#22512: Adding auto_now field to model causes migrate exception
---------------------------------+--------------------------------------
Reporter: jaddison | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: 1.7-beta-1
Severity: Release blocker | Resolution: duplicate
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Changes (by timo):

* status: new => closed
* resolution: => duplicate


Comment:

I confirmed this has been fixed. I am not sure about the issue of using
`timezone.now()` but it should be a separate ticket.

--
Ticket URL: <https://code.djangoproject.com/ticket/22512#comment:3>

Django

unread,
Apr 28, 2014, 8:49:44 AM4/28/14
to django-...@googlegroups.com
#22512: Adding auto_now field to model causes migrate exception
---------------------------------+--------------------------------------
Reporter: jaddison | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: 1.7-beta-1
Severity: Release blocker | Resolution: duplicate
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------

Comment (by timo):

You can read about the state of time zones in migrations #21954 (tldr;
they're not supported right now).

--
Ticket URL: <https://code.djangoproject.com/ticket/22512#comment:4>

Reply all
Reply to author
Forward
0 new messages