Python Django在删除迁移文件和迁移表后未创建数据库表

Python Django在删除迁移文件和迁移表后未创建数据库表,python,django,Python,Django,我通过django模型创建了两个表profile和details,但我错误地删除了这两个表。 当我尝试使用 python2.7 manage.py makemigrations 及 然后它没有创建表,所以我删除了迁移文件并截断了django_迁移表。现在当我运行命令时 python2.7 manage.py runserver 这给了我错误 You have unapplied migrations; your app may not work properly until they are

我通过django模型创建了两个表profile和details,但我错误地删除了这两个表。 当我尝试使用

python2.7 manage.py makemigrations

然后它没有创建表,所以我删除了迁移文件并截断了django_迁移表。现在当我运行命令时

python2.7 manage.py runserver
这给了我错误

You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
如果我运行python manage.py migrate命令,错误是

Operations to perform:
Apply all migrations: home, contenttypes, auth, sessions
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 342, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 200, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 92, in migrate
self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 198, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 123, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/models.py", line 59, in database_forwards
schema_editor.create_model(model)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 284, in create_model
self.execute(sql, params or None)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 110, in execute
cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "django_content_type" already exists 
运行以下命令:

python manage.py migrate --fake-initial

您需要首先为每个内置应用程序伪造迁移

python manage.py迁移-假身份验证 python manage.py迁移-伪内容类型

这首歌的首字母是假的

python manage.py migrate-假首字母


这很可能适合您。

现在显示django_内容_类型_应用_标签_76bd3d3b_uniq已经存在。您可能希望重命名整个数据库并再次运行迁移。这是正确的方法吗?因为我不能重命名数据库中的所有表,而且每个表中都有一些数据。有其他选择吗?即使您从models.py和migrations中删除了表,但数据库中已经存在这些表,您也可以从数据库中删除表,并从migration中删除迁移table@Dimitris运行此命令后,我首先从数据库中删除了表。错误为:-django.db.utils.ProgrammingError:关系的列名django_content_类型不存在删除整个数据库并创建新数据库,然后运行migrat
python manage.py migrate --fake-initial