#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/051.0c712ffb23866d74068e72c94bf9a1f6%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to