-1

After doing some extensive changes in the DB schema, I ran makemigrations. It created migrations successfully. But then migrate failed with:

AttributeError: 'str' object has no attribute '_meta'

What went wrong?

Here is the full traceback:

$ ./manage.py migrate
Operations to perform:
  Apply all migrations: account, admin, auth, clients, contenttypes, ...<snip>
Running migrations:
  Applying clients.0004_auto_20200910_1241...Traceback (most recent call last):
  File "./manage.py", line 21, in <module>
    main()
  File "./manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "...<snip>.../lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "...<snip>.../lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "...<snip>.../lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "...<snip>.../lib/python3.8/site-packages/django/core/management/base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "...<snip>.../lib/python3.8/site-packages/django/core/management/base.py", line 85, in wrapped
    res = handle_func(*args, **kwargs)
  File "...<snip>.../lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 243, in handle
    post_migrate_state = executor.migrate(
  File "...<snip>.../lib/python3.8/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 "...<snip>.../lib/python3.8/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 "...<snip>.../lib/python3.8/site-packages/django/db/migrations/executor.py", line 227, in apply_migration
    state = migration.apply(state, schema_editor)
  File "...<snip>.../lib/python3.8/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "...<snip>.../lib/python3.8/site-packages/django/db/migrations/operations/fields.py", line 236, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "...<snip>.../lib/python3.8/site-packages/django/db/backends/sqlite3/schema.py", line 138, in alter_field
    super().alter_field(model, old_field, new_field, strict=strict)
  File "...<snip>.../lib/python3.8/site-packages/django/db/backends/base/schema.py", line 553, in alter_field
    old_field.remote_field.through._meta.auto_created and
AttributeError: 'str' object has no attribute '_meta'

Somewhat similar to this question: Django makemigrations AttributeError: 'str' object has no attribute '_meta' but my makemigrations went ok and only failed at migrate command.

frnhr
  • 12,354
  • 9
  • 63
  • 90
  • I can make the same exception by executing ***`"foo"._meta`***. I would like to see *more detailed info* that probably help someone to identify the question in a better way. – JPG Sep 10 '20 at 15:08
  • @ArakkalAbu Thank you for your comment. I added the full traceback. Though I think there is a guideline against putting long-ish snippets of text like this in the Q or A. Not sure what else to put, though. Suggestions welcome. – frnhr Sep 10 '20 at 15:11

1 Answers1

0

Turns out that in one of the migrations there were 2 changes created in the wrong order:

  1. a model being deleted
  2. an M2M field which used the deleted model for the through param was altered (to use a different "through model").

Deleting the migrations.DeleteModel step from the migration file solved the issue.

Of course, I had to create a new migration that would actually delete the stale model, but that went perfectly.

Not sure if simply moving the order of the migration actions in the migration file would resolve the issue, probably.

Could be a bug in Django, needs some more research.

frnhr
  • 12,354
  • 9
  • 63
  • 90