Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#28884 closed Bug (fixed)

RenameField crashes with AttributeError when renaming a ManyToManyField (sqlite3)

Reported by: Emanuele Di Giacomo Owned by: Simon Charette
Component: Migrations Version: 2.0
Severity: Release blocker Keywords: sqlite3 sqlite
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Original issue: https://groups.google.com/forum/#!topic/django-users/O7s658gIHTE

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'.

This is a regression in Django 2.0: the same migration with Django 1.11 terminates successfully

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

Change History (6)

comment:1 by Simon Charette, 7 years ago

Owner: changed from nobody to Simon Charette
Status: newassigned
Triage Stage: UnreviewedAccepted

comment:2 by Tim Graham, 7 years ago

Summary: Regression in Django 2.0: RenameField throws AttributeErrorRenameField crashes with AttributeError when renaming a ManyToManyField

comment:4 by Ramiro Morales, 7 years ago

Keywords: sqlite3 sqlite added
Summary: RenameField crashes with AttributeError when renaming a ManyToManyFieldRenameField crashes with AttributeError when renaming a ManyToManyField (sqlite3)

comment:5 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: assignedclosed

In 9f7772e:

Fixed #28884 -- Fixed crash on SQLite when renaming a field in a model referenced by a ManyToManyField.

Introspected database constraints instead of relying on _meta.related_objects
to determine whether or not a table or a column is referenced on rename
operations.

This has the side effect of ignoring both db_constraint=False and virtual
fields such as GenericRelation which aren't backend by database level
constraints and thus shouldn't prevent the rename operations from being
performed in a transaction.

Regression in 095c1aaa898bed40568009db836aa8434f1b983d.

Thanks Tim for the additional tests and edits, and Mariusz for the review.

comment:6 by Tim Graham <timograham@…>, 7 years ago

In b40a1d7:

[2.0.x] Fixed #28884 -- Fixed crash on SQLite when renaming a field in a model referenced by a ManyToManyField.

Introspected database constraints instead of relying on _meta.related_objects
to determine whether or not a table or a column is referenced on rename
operations.

This has the side effect of ignoring both db_constraint=False and virtual
fields such as GenericRelation which aren't backend by database level
constraints and thus shouldn't prevent the rename operations from being
performed in a transaction.

Regression in 095c1aaa898bed40568009db836aa8434f1b983d.

Thanks Tim for the additional tests and edits, and Mariusz for the review.

Backport of 9f7772e098439f9edea3d25ab127539fc514eeb2 from master

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