【问题标题】:Django migrate throws AttributeError: 'str' object has no attribute '_meta'Django migrate 抛出 AttributeError: 'str' object has no attribute '_meta'
【发布时间】:2020-12-29 02:24:54
【问题描述】:

在对 DB 架构进行了一些广泛的更改之后,我运行了 makemigrations。它成功创建了迁移。但是后来migrate 失败了:

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

出了什么问题?

这是完整的回溯:

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

有点类似于这个问题:Django makemigrations AttributeError: 'str' object has no attribute '_meta' 但我的makemigrations 没问题,只是在migrate 命令失败。

【问题讨论】:

  • 我可以通过执行 "foo"._meta 来做出同样的例外。我希望看到更详细的信息,这可能有助于某人以更好的方式识别问题。
  • @ArakkalAbu 感谢您的评论。我添加了完整的回溯。虽然我认为有一个指导方针,反对在 Q 或 A 中放置像这样的长的 sn-ps 文本。不过,不知道还能放什么。欢迎提出建议。

标签: django django-migrations


【解决方案1】:

事实证明,在其中一次迁移中,以错误的顺序创建了 2 个更改:

  1. 一个模型被删除
  2. 已更改将已删除模型用于 through 参数的 M2M 字段(以使用不同的“直通模型”)。

从迁移文件中删除migrations.DeleteModel 步骤解决了该问题。

当然,我必须创建一个新的迁移,实际上会删除陈旧的模型,但这很完美。

不确定在迁移文件中简单地移动迁移操作的顺序是否可以解决问题。

可能是 Django 中的一个错误,需要更多研究。

【讨论】:

  • 这是怎么回事?
猜你喜欢
  • 2020-04-26
  • 2019-06-09
  • 2017-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-01
相关资源
最近更新 更多