Django postgres 关系不存在错误

新手上路,请多包涵

我知道这个问题有多少问题,但建议的解决方案都没有帮助。我遇到以下问题:我删除了我的模型并删除了 admin.py 中的导入。然后我想用 makemigrations 更新 django 数据库,这表明它删除了模型员工,但是在使用 migrate 命令后,我看到了一个错误,表明关系不存在。我还从我使用的 postgres 数据库中删除了它。这里奇怪的是 migrate 一直在寻找不存在的模型。 Even if I create another one makemigrations detects it and then migrate says relation project_employee.. Tried syncdb --all no success.这是 migrate 的输出

Apply all migrations: sessions, project, admin, auth, contenttypes
Running migrations:
Applying project.0003_auto_20160318_1215...Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "project_employee" does not exist

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
 File "manage.py", line 10, in <module>
  execute_from_command_line(sys.argv)
File "/usr/lib/python3/dist-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/usr/lib/python3/dist-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/lib/python3/dist-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/lib/python3/dist-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/usr/lib/python3/dist-packages/django/core/management/commands/migrate.py", line 161, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "/usr/lib/python3/dist-packages/django/db/migrations/executor.py", line 68, in migrate
self.apply_migration(migration, fake=fake)
File "/usr/lib/python3/dist-packages/django/db/migrations/executor.py", line 102, in apply_migration
migration.apply(project_state, schema_editor)
File "/usr/lib/python3/dist-packages/django/db/migrations/migration.py", line 108, in apply
operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
 File "/usr/lib/python3/dist-packages/django/db/migrations/operations/fields.py", line 84, in database_forwards
schema_editor.remove_field(from_model, from_model._meta.get_field_by_name(self.name)[0])
 File "/usr/lib/python3/dist-packages/django/db/backends/schema.py", line 431, in remove_field
self.execute(sql)
 File "/usr/lib/python3/dist-packages/django/db/backends/schema.py", line 111, in execute
cursor.execute(sql, params)
 File "/usr/lib/python3/dist-packages/django/db/backends/utils.py", line 81, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
 File "/usr/lib/python3/dist-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
 File "/usr/lib/python3/dist-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
 File "/usr/lib/python3/dist-packages/django/utils/six.py", line 658, in reraise
raise value.with_traceback(tb)
 File "/usr/lib/python3/dist-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
 django.db.utils.ProgrammingError: relation "project_employee" does not exist

`

谢谢

原文由 Nick 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 597
2 个回答

最终有帮助的是从项目文件夹中删除整个迁移文件夹。也看到了一些南方的反应,但没试过。

原文由 Nick 发布,翻译遵循 CC BY-SA 3.0 许可协议

AFAIK,您不应该在迁移前直接从数据库中删除表。如果你改变你的模型,然后 manage.py migrate 会做的事情。

django无法检测到DB的直接变化;只知道模型脚本的变化。因此,如果你删除一个表,那么 django 不会检测到更改,因此 django 会继续寻找被删除的表并给出错误。

有时迁移会无缘无故不起作用。在那种情况下,我会做以下事情:

  1. 撤消models.py的变化
  2. 做 django 迁移( manage.py makemigrations appnamemanage.py makemigrations --- 效果更好)
  3. 如果迁移有效,则再次更改 models.py
  4. 再次进行 django 迁移

这有时有效。

原文由 Leonard2 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏