#28073: RemoveField.state_forwards() crashes with AttributeError: 'NoneType'
object
has no attribute 'is_relation'
---------------------------------+------------------------------------
Reporter: Logan Gunthorpe | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.11
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Python Force):
* status: closed => new
* resolution: needsinfo =>
Old description:
> Hi,
>
> I'm trying to upgrade my project (originally created on 1.7) to the
> latest release but there's an issue with my existing migration files not
> working. The backtrace is below.
>
> I'll attache my migration files but it seems to be related to the
> migration removing the 'id' field which was automatically created and
> then removed in a future migration. Commenting out the RemoveField in the
> second migration seems to fix the issue but I'm not sure if it's entirely
> correct to do so.
>
> Thanks,
>
> Logan
>
>
> {{{
> Traceback (most recent call last):
> File "./manage.py", line 10, in <module>
> execute_from_command_line(sys.argv)
> File
> "/home/logang/projects/stove/software/website_dev/env/lib/python3.4/site-
> packages/django/core/management/__init__.py", line 363, in
> execute_from_command_line
> utility.execute()
> File
> "/home/logang/projects/stove/software/website_dev/env/lib/python3.4/site-
> packages/django/core/management/__init__.py", line 355, in execute
> self.fetch_command(subcommand).run_from_argv(self.argv)
> File
> "/home/logang/projects/stove/software/website_dev/env/lib/python3.4/site-
> packages/django/core/management/base.py", line 283, in run_from_argv
> self.execute(*args, **cmd_options)
> File
> "/home/logang/projects/stove/software/website_dev/env/lib/python3.4/site-
> packages/django/core/management/base.py", line 330, in execute
> output = self.handle(*args, **options)
> File
> "/home/logang/projects/stove/software/website_dev/env/lib/python3.4/site-
> packages/django/core/management/commands/migrate.py", line 163, in handle
> pre_migrate_state =
> executor._create_project_state(with_applied_migrations=True)
> File
> "/home/logang/projects/stove/software/website_dev/env/lib/python3.4/site-
> packages/django/db/migrations/executor.py", line 81, in
> _create_project_state
> migration.mutate_state(state, preserve=False)
> File
> "/home/logang/projects/stove/software/website_dev/env/lib/python3.4/site-
> packages/django/db/migrations/migration.py", line 92, in mutate_state
> operation.state_forwards(self.app_label, new_state)
> File
> "/home/logang/projects/stove/software/website_dev/env/lib/python3.4/site-
> packages/django/db/migrations/operations/fields.py", line 148, in
> state_forwards
> delay = not old_field.is_relation
> AttributeError: 'NoneType' object has no attribute 'is_relation'
>
> }}}
New description:
Hi,
I'm trying to upgrade my project (originally created on 1.7) to the latest
release but there's an issue with my existing migration files not working.
The backtrace is below.
I'll attache my migration files but it seems to be related to the
migration removing the 'id' field which was automatically created and then
removed in a future migration. Commenting out the RemoveField in the
second migration seems to fix the issue but I'm not sure if it's entirely
correct to do so.
Thanks,
Logan
{{{
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/logang/projects/stove/software/website_dev/env/lib/python3.4
/site-packages/django/core/management/__init__.py", line 363, in
execute_from_command_line
utility.execute()
File "/home/logang/projects/stove/software/website_dev/env/lib/python3.4
/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/logang/projects/stove/software/website_dev/env/lib/python3.4
/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/logang/projects/stove/software/website_dev/env/lib/python3.4
/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/home/logang/projects/stove/software/website_dev/env/lib/python3.4
/site-packages/django/core/management/commands/migrate.py", line 163, in
handle
pre_migrate_state =
executor._create_project_state(with_applied_migrations=True)
File "/home/logang/projects/stove/software/website_dev/env/lib/python3.4
/site-packages/django/db/migrations/executor.py", line 81, in
_create_project_state
migration.mutate_state(state, preserve=False)
File "/home/logang/projects/stove/software/website_dev/env/lib/python3.4
/site-packages/django/db/migrations/migration.py", line 92, in
mutate_state
operation.state_forwards(self.app_label, new_state)
File "/home/logang/projects/stove/software/website_dev/env/lib/python3.4
/site-packages/django/db/migrations/operations/fields.py", line 148, in
state_forwards
delay = not old_field.is_relation
AttributeError: 'NoneType' object has no attribute 'is_relation'
}}}
--
Comment:
I have the same issue like Logan.
Hello,
Was going from Django version 1.10.7 to 1.11.4 and my migrations were not
working. All my migrations were migrated before the upgrade. I went back
to 1.10.7 and it was working again. Going back to 1.11.4 same error.
{{{
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "[...]/venv/lib/python2.7/site-
packages/django/core/management/__init__.py", line 363, in
execute_from_command_line
utility.execute()
File "[...]/venv/lib/python2.7/site-
packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "[...]/venv/lib/python2.7/site-
packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "[...]/venv/lib/python2.7/site-
packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "[...]/venv/lib/python2.7/site-
packages/django/core/management/commands/migrate.py", line 163, in handle
pre_migrate_state =
executor._create_project_state(with_applied_migrations=True)
File "/[...]/venv/lib/python2.7/site-
packages/django/db/migrations/executor.py", line 81, in
_create_project_state
migration.mutate_state(state, preserve=False)
File "[...]/venv/lib/python2.7/site-
packages/django/db/migrations/migration.py", line 92, in mutate_state
operation.state_forwards(self.app_label, new_state)
File "[...]/venv/lib/python2.7/site-
packages/django/db/migrations/operations/fields.py", line 150, in
state_forwards
delay = not old_field.is_relation
AttributeError: 'NoneType' object has no attribute 'is_relation'
}}}
When I look in the fields.py file I can see on the line 139 a function:
{{{
def state_forwards(self, app_label, state):
new_fields = []
old_field = None
for name, instance in state.models[app_label,
self.model_name_lower].fields:
if name != self.name:
new_fields.append((name, instance))
else:
old_field = instance
state.models[app_label, self.model_name_lower].fields = new_fields
# Delay rendering of relationships if it's not a relational field
delay = not old_field.is_relation
state.reload_model(app_label, self.model_name_lower, delay=delay)
}}}
Found out that it was crashing on 1 of the model fields that are not
existing anymore in the database. I added a line to print the name of the
model and field to find out what is causing the crash.
If old_field = None then it is calling a method on empty variable and it
throws and error.
Added this line
print app_label + " " + self.model_name_lower + " " + self.name
{{{
def state_forwards(self, app_label, state):
new_fields = []
old_field = None
print app_label + " " + self.model_name_lower + " " + self.name
for name, instance in state.models[app_label,
self.model_name_lower].fields:
if name != self.name:
new_fields.append((name, instance))
else:
old_field = instance
state.models[app_label, self.model_name_lower].fields = new_fields
# Delay rendering of relationships if it's not a relational field
delay = not old_field.is_relation
state.reload_model(app_label, self.model_name_lower, delay=delay)
}}}
The Output was
{{{
cars video subtitle_url
cars photos gear
cars news tags
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "[...]/venv/lib/python2.7/site-
packages/django/core/management/__init__.py", line 363, in
execute_from_command_line
utility.execute()
File "[...]/venv/lib/python2.7/site-
packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "[...]/venv/lib/python2.7/site-
packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "[...]/venv/lib/python2.7/site-
packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "[...]/venv/lib/python2.7/site-
packages/django/core/management/commands/migrate.py", line 163, in handle
pre_migrate_state =
executor._create_project_state(with_applied_migrations=True)
File "/[...]/venv/lib/python2.7/site-
packages/django/db/migrations/executor.py", line 81, in
_create_project_state
migration.mutate_state(state, preserve=False)
File "[...]/venv/lib/python2.7/site-
packages/django/db/migrations/migration.py", line 92, in mutate_state
operation.state_forwards(self.app_label, new_state)
File "[...]/venv/lib/python2.7/site-
packages/django/db/migrations/operations/fields.py", line 150, in
state_forwards
delay = not old_field.is_relation
AttributeError: 'NoneType' object has no attribute 'is_relation'
}}}
That means it crashed on App[Cars] Model[News] and Field[Tags] - tags were
not in the DB anymore long time ago. I found this migration:
{{{
migrations.RemoveField(
model_name='news',
name='tags',
),
}}}
And deleted it.
After that the migration ran 100% and I am fully functional on 1.11.4.
For the record the function in 1.10.7 is - and that is working
{{{
def state_forwards(self, app_label, state):
new_fields = []
for name, instance in state.models[app_label,
self.model_name_lower].fields:
if name != self.name:
new_fields.append((name, instance))
state.models[app_label, self.model_name_lower].fields = new_fields
state.reload_model(app_label, self.model_name_lower)
}}}
1. fields.py
https://github.com/django/django/blob/master/django/db/migrations/operations/fields.py
2. Who was working on that one
https://github.com/django/django/commit/45ded053b1f4320284aa5dac63052f6d1baefea9
--
Ticket URL: <https://code.djangoproject.com/ticket/28073#comment:9>
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/065.7c4336b57ddd07d28e95b3030418aa8b%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.