Django:在 Django 1.9 版本中重新创建应用程序的所有表
Django : Recreating all the tables for app in Django 1.9 version
我已经尝试使用建议的 ./manage.py migrate app_name zero 命令,但我在 运行 python manage.py 之后一直出错迁移测试应用程序。我最后的解决方案是去 mysql drop entire 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")
如果您的数据库中已有表,请尝试./manage.py flush
清除所有数据。 Flush 对整个数据库执行 SQL Drops。
如果要删除数据库,请删除 mysql 数据库。创建另一个 mysql 具有相同名称的数据库。然后 运行 ./manage.py makemigrations
然后 ./manage.py migrate
重新创建数据库中的表。
您正在应用初始迁移,这意味着您正在创建该应用的第一个版本 table。
但是你已经在数据库中 table 所以你可以伪造初始迁移
python manage.py migrate --fake-initial
The --fake-initial flag to migrate was added in Django 1.8. Previously, Django would always automatically fake-apply initial migrations if it detected that the tables exist.
但请注意,这只适用于两件事:
- 自从您制作了他们的 table 模型(您已经拥有)后,您没有更改过您的模型。
- 您尚未手动编辑数据库。
所以你也不能伪造它们。
我想使用当前数据库
如果您之前未更改版本的 testapp 初始迁移文件包含经过您更改的迁移(非初始)文件,则可以使用它们进行迁移。
首先伪造初始迁移,然后应用您的更改
(现在这可能很棘手,因为您还必须截断存储迁移数据的 django_migrations table)
我可以从头开始 -
No Problem (drop your database) as its an initial migration and that's what they are for - Create DB/Table.
我已经尝试使用建议的 ./manage.py migrate app_name zero 命令,但我在 运行 python manage.py 之后一直出错迁移测试应用程序。我最后的解决方案是去 mysql drop entire 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")
如果您的数据库中已有表,请尝试./manage.py flush
清除所有数据。 Flush 对整个数据库执行 SQL Drops。
如果要删除数据库,请删除 mysql 数据库。创建另一个 mysql 具有相同名称的数据库。然后 运行 ./manage.py makemigrations
然后 ./manage.py migrate
重新创建数据库中的表。
您正在应用初始迁移,这意味着您正在创建该应用的第一个版本 table。
但是你已经在数据库中 table 所以你可以伪造初始迁移
python manage.py migrate --fake-initial
The --fake-initial flag to migrate was added in Django 1.8. Previously, Django would always automatically fake-apply initial migrations if it detected that the tables exist.
但请注意,这只适用于两件事:
- 自从您制作了他们的 table 模型(您已经拥有)后,您没有更改过您的模型。
- 您尚未手动编辑数据库。
所以你也不能伪造它们。
我想使用当前数据库
如果您之前未更改版本的 testapp 初始迁移文件包含经过您更改的迁移(非初始)文件,则可以使用它们进行迁移。
首先伪造初始迁移,然后应用您的更改 (现在这可能很棘手,因为您还必须截断存储迁移数据的 django_migrations table)
我可以从头开始 -
No Problem (drop your database) as its an initial migration and that's what they are for - Create DB/Table.