Django:在Django 1.9版本中重新创建app的所有表格

时间:2016-07-15 16:39:10

标签: django django-1.9

我尝试过使用./manage.py migrate app_name zero命令,但我在运行python manage.py migrate testapp后仍然遇到错误。我的最后一个解决方案是去mysql drop whole db

Operations to perform:
  Apply all migrations: testapp
Running migrations:
  Rendering model states... DONE
  Applying testapp.0001_initial...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python27\lib\site-packages\django-1.9.7-py2.7.egg\django\core\management\__init__.py", line 353,
_line
    utility.execute()
  File "C:\Python27\lib\site-packages\django-1.9.7-py2.7.egg\django\core\management\__init__.py", line 345,
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python27\lib\site-packages\django-1.9.7-py2.7.egg\django\core\management\base.py", line 348, in
    self.execute(*args, **cmd_options)
  File "C:\Python27\lib\site-packages\django-1.9.7-py2.7.egg\django\core\management\base.py", line 399, in
    output = self.handle(*args, **options)
  File "C:\Python27\lib\site-packages\django-1.9.7-py2.7.egg\django\core\management\commands\migrate.py", l
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "C:\Python27\lib\site-packages\django-1.9.7-py2.7.egg\django\db\migrations\executor.py", line 92, in
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\Python27\lib\site-packages\django-1.9.7-py2.7.egg\django\db\migrations\executor.py", line 121, i
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\Python27\lib\site-packages\django-1.9.7-py2.7.egg\django\db\migrations\executor.py", line 198, i
    state = migration.apply(state, schema_editor)
  File "C:\Python27\lib\site-packages\django-1.9.7-py2.7.egg\django\db\migrations\migration.py", line 123,
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Python27\lib\site-packages\django-1.9.7-py2.7.egg\django\db\migrations\operations\models.py", li
rds
    schema_editor.create_model(model)
  File "C:\Python27\lib\site-packages\django-1.9.7-py2.7.egg\django\db\backends\base\schema.py", line 284,
    self.execute(sql, params or None)
  File "C:\Python27\lib\site-packages\django-1.9.7-py2.7.egg\django\db\backends\base\schema.py", line 110,
    cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django-1.9.7-py2.7.egg\django\db\backends\utils.py", line 79, in exec
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Python27\lib\site-packages\django-1.9.7-py2.7.egg\django\db\backends\utils.py", line 64, in exec
    return self.cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django-1.9.7-py2.7.egg\django\db\utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Python27\lib\site-packages\django-1.9.7-py2.7.egg\django\db\backends\utils.py", line 62, in exec
    return self.cursor.execute(sql)
  File "C:\Python27\lib\site-packages\django-1.9.7-py2.7.egg\django\db\backends\mysql\base.py", line 112, i
    return self.cursor.execute(query, args)
  File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)
  File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
django.db.utils.OperationalError: (1050, "Table 'testapp_cinfo' already exists")

2 个答案:

答案 0 :(得分:1)

您正在应用初始迁移,这意味着您正在创建该应用程序表的第一个版本。

但是您已经在数据库中拥有该表,因此您可以伪造初始迁移

python manage.py migrate --fake-initial
  

在Django 1.8中添加了要迁移的--fake-initial标志。以前,如果检测到表存在,Django将始终自动伪造 - 应用初始迁移。

但请注意,这仅适用于两件事:

  1. 自从你制作了他们的桌子后,你没有改变你的模型。
  2. 您尚未手动编辑数据库。
  3. 所以你也不能伪造它们。

    • 我想使用当前的数据库

      如果您的testapp的初始迁移文件的先前未更改版本包含您的更改的迁移(非初始)文件,则可以使用它们进行迁移。

      首先伪造初始迁移,然后应用您的更改 (现在这可能很棘手,因为您还必须截断存储迁移数据的django_migrations表)

    • 我可以从头开始 -

        

      没有问题(删除数据库)作为初始迁移,这就是它们的用途 - 创建数据库/表。

答案 1 :(得分:0)

如果您的数据库中已有表,请尝试./manage.py flush清除所有数据。 Flush在整个数据库上执行SQL Drop。

如果要删除db,请删除mysql db。创建另一个具有相同名称的mysql数据库。然后运行./manage.py makemigrations,然后运行./manage.py migrate以重新创建数据库中的表。