Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#21785 closed Bug (fixed)

migrate fails on DateTimeField with auto_now AND auto_now_add set

Reported by: Gabe Jackson Owned by: Andrew Godwin
Component: Migrations Version: dev
Severity: Release blocker Keywords: migrations
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

migrate fails on DateTimeField(auto_now=True, auto_now_add=True):

Running migrations:
  No migrations needed.
Traceback (most recent call last):
  File "./manage.py", line 9, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/gabejackson/venv/m/lib/python2.7/site-packages/django/core/management/__init__.py", line 427, in execute_from_command_line
    utility.execute()
  File "/Users/gabejackson/venv/m/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/gabejackson/venv/m/lib/python2.7/site-packages/django/core/management/base.py", line 244, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/gabejackson/venv/m/lib/python2.7/site-packages/django/core/management/base.py", line 291, in execute
    output = self.handle(*args, **options)
  File "/Users/gabejackson/venv/m/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 138, in handle
    ProjectState.from_apps(apps),
  File "/Users/gabejackson/venv/m/lib/python2.7/site-packages/django/db/migrations/state.py", line 61, in from_apps
    model_state = ModelState.from_model(model)
  File "/Users/gabejackson/venv/m/lib/python2.7/site-packages/django/db/migrations/state.py", line 114, in from_model
    name, path, args, kwargs = field.deconstruct()
  File "/Users/gabejackson/venv/m/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 911, in deconstruct
    del kwargs['editable']
KeyError: u'editable'

This is due to: django/db/models/fields/init.py doing this:

    def deconstruct(self):
        name, path, args, kwargs = super(DateField, self).deconstruct()
        if self.auto_now:
            kwargs['auto_now'] = True
            del kwargs['editable']
            del kwargs['blank']
        if self.auto_now_add:
            kwargs['auto_now_add'] = True
            del kwargs['editable']
            del kwargs['blank']
        return name, path, args, kwargs

this should probably be:

    def deconstruct(self):
        name, path, args, kwargs = super(DateField, self).deconstruct()
        if self.auto_now or self.auto_now_add:
            del kwargs['editable']
            del kwargs['blank']
        if self.auto_now:
            kwargs['auto_now'] = True
        if self.auto_now_add:
            kwargs['auto_now_add'] = True
        return name, path, args, kwargs

Change History (5)

comment:1 by Andrew Godwin, 11 years ago

Owner: set to Andrew Godwin
Status: newassigned

comment:2 by Simon Charette, 11 years ago

I think specifying both auto_now and auto_add_now should be disallowed instead. It doesn't make much sense since auto_add_now is really just a case subset of auto_now.

comment:3 by Gabe Jackson, 11 years ago

for my defense... this was an autocomplete mistake i made in pycharm. Of course it doesn't make sense to define both auto_now and auto_now_add, but this should be caught when initializing the field.

comment:4 by Andrew Godwin <andrew@…>, 11 years ago

Resolution: fixed
Status: assignedclosed

In 86e69bf3b67ff1e156413e07cef018ec52c2cdd4:

Fixed #21785: Bad handling of both auto_now and _add on deconstruct

comment:5 by Simon Charette, 11 years ago

Tracking the auto_now and auto_add_now issue in #21798.

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