RenameField with django 2.0: AttributeError: 'ManyToManyRel' object has no attribute 'field_name'

527 views
Skip to first unread message

edigi...@arpae.it

unread,
Dec 4, 2017, 8:19:50 AM12/4/17
to Django users
With Django==2.0, a RenameField on a model which has a reverse many to many relationship raises an exception:

    AttributeError: 'ManyToManyRel' object has no attribute 'field_name'

The same migration with Django==1.11 terminates successfully.

Migrations are automatically generated by `manage.py makemigrations`.

Am I missing something?

Below the code and the commands to reproduce the problem:

# myapp/models.py

from django.db import models


class ModelA(models.Model):
    new_name = models.IntegerField()


class ModelB(models.Model):
    model_as = models.ManyToManyField('ModelA')

# myapp/migrations/0001_initial.py

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='ModelA',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('old_name', models.IntegerField()),
            ],
        ),
        migrations.CreateModel(
            name='ModelB',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('model_as', models.ManyToManyField(to='myapp.ModelA')),
            ],
        ),
    ]

# myapp/migrations/0002_auto_20171204_1012.py

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('myapp', '0001_initial'),
    ]

    operations = [
        migrations.RenameField(
            model_name='modela',
            old_name='old_name',
            new_name='new_name',
        ),
    ]

$ ./manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, myapp, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying myapp.0001_initial... OK
  Applying myapp.0002_auto_20171204_1012...Traceback (most recent call last):
  File "./manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/edg/src/example/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/home/edg/src/example/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/edg/src/example/env/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/edg/src/example/env/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "/home/edg/src/example/env/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    fake_initial=fake_initial,
  File "/home/edg/src/example/env/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/edg/src/example/env/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/edg/src/example/env/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/edg/src/example/env/lib/python3.6/site-packages/django/db/migrations/migration.py", line 122, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/edg/src/example/env/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 304, in database_forwards
    to_model._meta.get_field(self.new_name),
  File "/home/edg/src/example/env/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 81, in alter_field
    any(r.field_name == old_field.name for r in model._meta.related_objects)):
  File "/home/edg/src/example/env/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 81, in <genexpr>
    any(r.field_name == old_field.name for r in model._meta.related_objects)):
AttributeError: 'ManyToManyRel' object has no attribute 'field_name'

$ pip freeze
Django==2.0
pytz==2017.3

Simon Charette

unread,
Dec 4, 2017, 12:00:30 PM12/4/17
to Django users
Hello there,

This looks like a regression in Django 2.0 caused by 095c1aaa[0].

Please file a new ticket mentioning it's a regression in Django 2.0
and mark it as a release blocker. [1]

Thanks,
Simon

[0] https://github.com/django/django/commit/095c1aaa898bed40568009db836aa8434f1b983d
[1] https://code.djangoproject.com/newticket

edigi...@arpae.it

unread,
Dec 4, 2017, 1:11:49 PM12/4/17
to Django users
Thanks Simon,

   Emanuele
Reply all
Reply to author
Forward
0 new messages