如何解决Django迁移引发AttributeError:'str'对象没有属性'_meta'
在数据库架构中进行了一些广泛的更改之后,我运行了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,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
命令下失败了。
解决方法
事实证明,在其中一个迁移中,以错误的顺序创建了2个更改:
- 正在删除的模型
- 使用了
through
参数的已删除模型的M2M字段已更改(以使用其他“通过模型”)。
从迁移文件中删除migrations.DeleteModel
步骤可以解决此问题。
当然,我必须创建一个新的迁移,该迁移实际上会删除过时的模型,但是效果很好。
不确定仅移动迁移文件中迁移操作的顺序是否可以解决问题。
可能是Django中的错误,需要更多研究。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。