Code

Opened 6 months ago

Closed 6 months ago

Last modified 6 months ago

#21785 closed Bug (fixed)

migrate fails on DateTimeField with auto_now AND auto_now_add set

Reported by: gabejackson Owned by: andrewgodwin
Component: Migrations Version: master
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

Attachments (0)

Change History (5)

comment:1 Changed 6 months ago by andrewgodwin

  • Needs documentation unset
  • Needs tests unset
  • Owner set to andrewgodwin
  • Patch needs improvement unset
  • Status changed from new to assigned

comment:2 Changed 6 months ago by charettes

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 Changed 6 months ago by gabejackson

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 Changed 6 months ago by Andrew Godwin <andrew@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In 86e69bf3b67ff1e156413e07cef018ec52c2cdd4:

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

comment:5 Changed 6 months ago by charettes

Tracking the auto_now and auto_add_now issue in #21798.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.