Django迁移引发AttributeError:'str'对象没有属性'_meta'

时间:2020-09-10 14:45:57

标签: django django-migrations

在数据库架构中进行了一些广泛的更改之后,我运行了16:39:22.181 PGSQL Resource "opentap @ localhost:5433" opened. [173 ms] 16:39:22.181 Summary ------ Summary of test plan started 09/10/2020 16:39:22 ------ 16:39:22.181 Summary -------------------------------------------------------------- 16:39:22.181 Summary ----- Test plan completed with verdict Error in 2.33 ms ------ 16:39:22.210 PGSQL Error in OnTestPlanRunCompleted for 'opentap @ localhost:5433': '23503: insert or update on table "testrun2attachment" violates foreign key constraint "testrun2attachment_runid_fkey"' 16:39:22.216 PGSQL Resource "opentap @ localhost:5433" closed. [1.22 ms] 。它成功创建了迁移。但是随后makemigrations失败了:

migrate

出了什么问题?

这是完整的追溯:

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

与这个问题有点类似:Django makemigrations 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' 没问题,只在makemigrations命令下失败了。

1 个答案:

答案 0 :(得分:0)

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

  1. 正在删除的模型
  2. 使用了through参数的已删除模型的M2M字段已更改(以使用其他“通过模型”)。

从迁移文件中删除migrations.DeleteModel步骤可以解决此问题。

当然,我必须创建一个新的迁移,该迁移实际上会删除过时的模型,但是效果很好。

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

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