#22512 closed Bug (duplicate)
Adding auto_now field to model causes migrate exception
| Reported by: | James Addison | 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 |
Description
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=Trueset - Run
makemigrationscommand - Run
migratecommand
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.
Change History (4)
comment:1 by , 12 years ago
comment:2 by , 12 years ago
I believe this was fixed with 074d3183d92cd5ed5da8f51e7048b12a96f55e0a, could you try the master branch?
comment:3 by , 12 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | new → closed |
I confirmed this has been fixed. I am not sure about the issue of using timezone.now() but it should be a separate ticket.
comment:4 by , 12 years ago
You can read about the state of time zones in migrations #21954 (tldr; they're not supported right now).
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: