Django + posgtres关系不存在错误

时间:2016-03-18 15:00:14

标签: python django postgresql

我理解这个问题有多少问题,但没有一个解决方案有帮助。 我遇到了以下问题: 我删除了我的模型并删除了admin.py中的导入。然后我想用makemigrations更新django数据库,这表明它删除了模型员工,但是在使用migrate命令后,我看到了一个错误,表明该关系不存在。我也把它从我使用的postgres数据库中删除了。 这里奇怪的是迁移一直在寻找不存在的模型。即使我创建另一个makemigrations检测到它,然后migraterelation project_employee.. 试过syncdb --all没有成功。这是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

`

谢谢

6 个答案:

答案 0 :(得分:2)

我可以通过以下步骤解决此问题

  1. 当我运行此命令时

    python manage.py migrate app_name zero

    它在抱怨缺少某些表。所以我创建了一个带有虚拟列的虚拟表。

  2. 我再次运行了命令

    python manage.py migrate app_name zero

  3. 我已为该应用应用了迁移

    python manage.py migrate app_name

答案 1 :(得分:1)

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

答案 2 :(得分:1)

AFAIK,您不应在迁移之前直接从数据库中删除表。如果您更改了模型,那么manage.py migrate将会执行此操作。

django无法检测到DB的直接变化;只知道模型脚本的变化。因此,如果删除表,则django不会检测到更改,因此django会一直查找已删除的表并给出错误。

有时候迁移无缘无故。在这种情况下,我会做以下事情:

  1. 撤消models.py
  2. 的更改
  3. 执行django迁移(manage.py makemigrations appnamemanage.py makemigrations更好)
  4. 如果迁移有效,则再次更改models.py
  5. 再次进行django迁移
  6. 这有时会奏效。

答案 3 :(得分:1)

使用以下代码在数据库中删除表

python manage.py migrate app_name zero

然后再次迁移

python manage.py migrate app_name

原因是已经存在一个表,并且当您执行“初始迁移”时,Django将看到已经应用了初始迁移,因为该表已经存在于旧模式中,因此不采用新模式具有不同架构的表格。

答案 4 :(得分:0)

我需要做的就是创建一个具有相同名称和一个虚拟列的表。然后当我迁移时,django为我重新制作了所有列。

答案 5 :(得分:-2)

最后这对我有用:

import argparse
import shlex

parser = argparse.ArgumentParser(prog="whatever", description='test argp')
parser.add_argument("--test", help="Test")    

cmd = '--test something'
cmdline = shlex.split(cmd)
         
pargs =  parser.parse_args(cmdline)