Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'ManyToManyField' object has no attribute 'm2m_reverse_field_name'

I'm trying to run a migration for my Django project, but I'm getting the error:

AttributeError: 'ManyToManyField' object has no attribute 'm2m_reverse_field_name'

I when I ran make migrations on all my apps, I didn't get any errors. It's only when I try to actually migrate. I can't tell from the traceback information which model is creating the problem, or even which app. I've looked at my models, and I don't see anything that pops out at me.

Here is the stack trace:

Operations to perform:
  Apply all migrations: admin, sessions, case_manager, file_manager, auth, contenttypes, tasks, people_and_property
Running migrations:
  Rendering model states... DONE
  Applying file_manager.0006_auto_20160109_1536...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
    utility.execute()
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 342, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/db/migrations/executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/db/migrations/migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 467, in alter_field
    return self._alter_many_to_many(model, old_field, new_field, strict)
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/db/backends/sqlite3/schema.py", line 274, in _alter_many_to_many
    old_field.remote_field.through._meta.get_field(old_field.m2m_reverse_field_name()),
AttributeError: 'ManyToManyField' object has no attribute 'm2m_reverse_field_name'

How do I figure out which model is the problem? What should I look for?

like image 605
jejy2343 Avatar asked Jan 09 '16 20:01

jejy2343


2 Answers

You have to make sure that the model you are creating the 'ManyToManyField' is already created in the database.

You can do that by adding as a dependency the migration where the model is created to your migration in which you alter the field:

Scenario 1: You alter the field to 'ManyToManyField' with a model from other app

class Migration(migrations.Migration):

    dependencies = [
      ..........
      ('[app]', '__first__'),
    ]

    operations = [
       .........
    ]

Scenario 2: You create a 'ManyToManyField' and the model you are referring to is in the same file:

 class Migration(migrations.Migration):

    dependencies = [
      ..........
    ]

    operations = [
       .........
       # Make sure the model you are making the reference with is  before the ManyToManyField
       migrations.CreateModel(...) ,
       migrations.AlterField/CreateField(...)

    ]
like image 173
jsep Avatar answered Sep 22 '22 13:09

jsep


I ran into the same problem, but I don’t know if for the same reasons. Luckily I don’t have any important data in the system, so I just changed the migration as follows – but note that this deletes all data in these columns!

Before:

operations = [
    migrations.AlterField(
        model_name='resource',
        name='authors',
        field=models.ManyToManyField(related_name='resources_authored', to='api.Person'),
    ),
    migrations.AlterField(
        model_name='resource',
        name='editors',
        field=models.ManyToManyField(blank=True, related_name='resources_edited', to='api.Person'),
    ),
]

After:

operations = [
    migrations.RemoveField(
        model_name='resource',
        name='authors',
    ),
    migrations.RemoveField(
        model_name='resource',
        name='editors',
    ),
    migrations.AddField(
        model_name='resource',
        name='authors',
        field=models.ManyToManyField(related_name='resources_authored', to='api.Person'),
    ),
    migrations.AddField(
        model_name='resource',
        name='editors',
        field=models.ManyToManyField(blank=True, related_name='resources_edited', to='api.Person'),
    ),
]

While the altering failed for mysterious reasons, removing and recreating the fields worked.

like image 42
Denis Drescher Avatar answered Sep 18 '22 13:09

Denis Drescher