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

如何解决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个更改:

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

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

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?