Opened 19 months ago

Closed 19 months ago

Last modified 19 months ago

#22512 closed Bug (duplicate)

Adding auto_now field to model causes migrate exception

Reported by: jaddison Owned by: nobody
Component: Migrations Version: 1.7-beta-1
Severity: Release blocker Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


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)$ ./ 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
Select an option: 1       
Please enter the default value now, as valid Python
The datetime module is available, so you can do e.g.
Migrations for 'content':
    - Add field updated to contentlink

Calling migrate:

(project1)jaddison@jamess-air-2:~/projects/project1 (master)$ ./ migrate
Traceback (most recent call last):
  File "./", line 10, in <module>
  File "/Users/jaddison/.virtualenvs/project1/lib/python2.7/site-packages/django/core/management/", line 427, in execute_from_command_line
  File "/Users/jaddison/.virtualenvs/project1/lib/python2.7/site-packages/django/core/management/", line 419, in execute
  File "/Users/jaddison/.virtualenvs/project1/lib/python2.7/site-packages/django/core/management/", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/jaddison/.virtualenvs/project1/lib/python2.7/site-packages/django/core/management/", line 337, in execute
    output = self.handle(*args, **options)
  File "/Users/jaddison/.virtualenvs/project1/lib/python2.7/site-packages/django/core/management/commands/", line 62, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/Users/jaddison/.virtualenvs/project1/lib/python2.7/site-packages/django/db/migrations/", line 14, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/Users/jaddison/.virtualenvs/project1/lib/python2.7/site-packages/django/db/migrations/", line 48, in __init__
  File "/Users/jaddison/.virtualenvs/project1/lib/python2.7/site-packages/django/db/migrations/", line 145, in build_graph
  File "/Users/jaddison/.virtualenvs/project1/lib/python2.7/site-packages/django/db/migrations/", 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/", line 37, in import_module
  File "/Users/jaddison/projects/project1/content/migrations/", line 6, in <module>
    class Migration(migrations.Migration):
  File "/Users/jaddison/projects/project1/content/migrations/", 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

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

class Migration(migrations.Migration):

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

    operations = [
            field=models.DateTimeField(default=datetime(2014, 4, 25, 4, 32, 56, 89929), auto_now=True),

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

Change History (4)

comment:1 Changed 19 months ago by jaddison

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

As an additional note, would it not be wise to use if time zone support is active? (see

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/ RuntimeWarning: SQLite received a naive datetime (2014-04-25 04:32:56.089929) while time zone support is active.

comment:2 Changed 19 months ago by loic84

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

comment:3 Changed 19 months ago by timo

  • Resolution set to duplicate
  • Status changed from new to closed

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

comment:4 Changed 19 months ago by timo

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

Note: See TracTickets for help on using tickets.
Back to Top