另一个南方“表不存在”问题:以前发布的解决方案都没有工作

时间:2013-08-24 05:35:00

标签: django django-models django-south database-migration django-settings

我正在尝试使用Django创建应用程序并使用South来处理迁移。在我定义应用程序的models.py之后,我在settings.py中的“INSTALLED_APPS”中包含了south。然后我同步我的数据库。当我验证数据库时,我得到0错误。然后我在命令提示符下执行以下命令:

C:\Users\abagaria\Desktop\IntegrateID\website>python manage.py schemamigration w
ebsite.integrate --initial
Creating migrations directory at 'C:\Users\abagaria\Desktop\IntegrateID\website\
website\integrate\migrations'...
Creating __init__.py in 'C:\Users\abagaria\Desktop\IntegrateID\website\website\i
ntegrate\migrations'...
 + Added model integrate.Publisher
 + Added model integrate.Author
 + Added model integrate.Book
 + Added M2M table for authors on integrate.Book
Created 0001_initial.py. You can now apply this migration with: ./manage.py migr
ate integrate

C:\Users\abagaria\Desktop\IntegrateID\website>python manage.py migrate website.i
ntegrate
Running migrations for integrate:
 - Migrating forwards to 0001_initial.
 > integrate:0001_initial
FATAL ERROR - The following SQL query failed: CREATE TABLE "integrate_publisher"
 ("id" integer NOT NULL PRIMARY KEY, "name" varchar(30) NOT NULL, "address" varc
har(50) NOT NULL, "city" varchar(60) NOT NULL, "state_province" varchar(30) NOT
NULL, "country" varchar(50) NOT NULL, "website" varchar(200) NOT NULL)
The error was: table "integrate_publisher" already exists
 ! Error found during real run of migration! Aborting.

 ! Since you have a database that does not support running
 ! schema-altering statements in transactions, we have had
 ! to leave it in an interim state between migrations.

! You *might* be able to recover with:   = DROP TABLE "integrate_publisher"; []
   = DROP TABLE "integrate_author"; []
   = DROP TABLE "integrate_book"; []
   = DROP TABLE "integrate_book_authors"; []

 ! The South developers regret this has happened, and would
 ! like to gently persuade you to consider a slightly
 ! easier-to-deal-with DBMS (one that supports DDL transactions)
 ! NOTE: The error which caused the migration to fail is further up.
Error in migration: integrate:0001_initial
DatabaseError: table "integrate_publisher" already exists

我知道很多人在使用南方时遇到过类似的问题,但通常在他们的情况下,他们犯了多次执行“ - 初始”命令的错误 - 从而导致南方不止一次迁移目录中的__initial文件。但就我而言,南方认为即使我第一次迁移,该表已经存在!

我也尝试过:

  • 删除迁移目录
  • 删除幽灵迁移
  • 进行“虚假”迁移
  • 然后运行实际迁移

有人可以告诉我如何解决这个问题,并且可以再次开始定义我的模型吗?

1 个答案:

答案 0 :(得分:1)

如果您已经在数据库中有表,请不要使用--initial,而是需要convert_to_south命令。删除目录“迁移”,数据库中的所有表并运行以下命令:

python manage.py syncdb
python manage.py convert_to_south appname
python manage.py syncdb --migrate

http://south.readthedocs.org/en/latest/convertinganapp.html