我理解这个问题有多少问题,但没有一个解决方案有帮助。
我遇到了以下问题:
我删除了我的模型并删除了admin.py
中的导入。然后我想用makemigrations
更新django数据库,这表明它删除了模型员工,但是在使用migrate
命令后,我看到了一个错误,表明该关系不存在。我也把它从我使用的postgres数据库中删除了。
这里奇怪的是迁移一直在寻找不存在的模型。即使我创建另一个makemigrations
检测到它,然后migrate
说relation 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
`
谢谢
答案 0 :(得分:2)
我可以通过以下步骤解决此问题
当我运行此命令时
python manage.py migrate app_name zero
它在抱怨缺少某些表。所以我创建了一个带有虚拟列的虚拟表。
我再次运行了命令
python manage.py migrate app_name zero
我已为该应用应用了迁移
python manage.py migrate app_name
答案 1 :(得分:1)
最终帮助从项目文件夹中删除整个迁移文件夹。看到一些南方的回应,但没有尝试过。
答案 2 :(得分:1)
AFAIK,您不应在迁移之前直接从数据库中删除表。如果您更改了模型,那么manage.py migrate
将会执行此操作。
有时候迁移无缘无故。在这种情况下,我会做以下事情:
manage.py makemigrations appname
比manage.py makemigrations
更好)这有时会奏效。
答案 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)