Why don't my south migrations work?

First, I create my database.

create database mydb;

我将“南方”添加到已安装的应用程序。然后,我转到此教程:http://south.aeracode.org/docs/tutorial/part1.html

The tutorial tells me to do this:

$ py manage.py  schemamigration wall --initial
>>> Created 0001_initial.py. You can now apply this migration with: ./manage.py migrate wall

Great, now I migrate.

$ py manage.py migrate wall

But it gives me this error...

django.db.utils.DatabaseError: (1146, "Table 'fable.south_migrationhistory' doesn't exist")

So I use Google (which never works. hence my 870 questions asked on Stackoverflow), and I get this page: http://groups.google.com/group/south-users/browse_thread/thread/d4c83f821dd2ca1c

Alright, so I follow that instructions

>> Drop database mydb;
>> Create database mydb;
$ rm -rf ./wall/migrations
$ py manage.py syncdb

But when I run syncdb, Django creates a bunch of tables. Yes, it creates the south_migrationhistory table, but it also creates my app's tables.

Synced:
> django.contrib.admin
> django.contrib.auth
> django.contrib.contenttypes
> django.contrib.sessions
> django.contrib.sites
> django.contrib.messages
> south
> fable.notification
> pagination
> timezones
> fable.wall
> mediasync
> staticfiles
> debug_toolbar


Not synced (use migrations):
-
(use ./manage.py migrate to migrate these)

Cool....now it tells me to migrate these. So, I do this:

$ py manage.py  migrate wall
The app 'wall' does not appear to use migrations.

Alright, so fine. I'll add wall to initial migrations.

$ py manage.py schemamigration wall --initial

Then I migrate:

$ py manage.py migrate wall

You know what? It gives me this BS:

_mysql_exceptions.OperationalError: (1050, "Table 'wall_content' already exists")

Sorry, this is really pissing me off. Can someone help ? thanks.

How do I get South to work and sync correctly with everything? The only thing I can think of is remove my app from INSTALLED_APPS, then run syncdb, then add it back on.

That is SO SILLY.

48675 次浏览

你正在使用的教程指出:

(如果这种做法失败, south_migrationhistory does not exist, 您忘记在后面运行 syncdb < a href = “ http://south.aeracode.org/docs/installation.html # install-configure”rel = “ norefrer”> 安装 南方

假设你的文章准确地详细说明了你所采取的步骤,那么点击这个链接似乎表明你在设置新应用程序之前漏掉了一个步骤。如果您正在学习在新应用程序上设置迁移的教程,顺序是:

  1. 南边添加到 INSTALLED_APPS
  2. 运行 syncdb
  3. 然后按照教程操作。

I.e., you should've already run syncdb before you added in the models for your new app. Your solution of removing your app from INSTALLED_APPS should work, but it's worth noting that it's really only a "silly" work-around, as you missed a step earlier on. Had syncdb been run before you created the models for that app, you wouldn't have to use the work-around.

我怎么才能让南边工作并同步呢 一切都正确吗? 唯一的 我能想到的就是删除我的应用程序 然后运行 syncdb, 然后再加回去。

我以前也用过这种方法来解决南方的问题,虽然不是一个很好的解决方案,但是非常有效;)

But the main problem is that your order isn't correct. You should have run syncdb before the tutorial. Than it works properly.

South 允许您在开始使用一个新应用程序时创建迁移,而且这些表还没有添加到数据库中,以及为已经在数据库中有表的遗留应用程序创建迁移。关键是知道什么时候该做什么。

Your first mistake was when you deleted your migrations, as soon as you did that, and then ran syncdb, Django didn't know that you wanted south to manage that app anymore, so it created the tables for you. When you created your initial migrations and then ran migrate, south was trying to create tables that django already created, and thus your error.

现在你有两个选择。

  1. 从数据库中删除 wall 应用程序的表,然后运行 $ py manage.py migrate wall。这将运行迁移并创建表。

  2. 伪造初始迁移运行 $ py manage.py migrate wall 0001 --fake这将告诉 South 数据库中已经有了表,所以只需要伪造一下,这将向 south _  中添加一行,以便下次您運行迁移時它知道第一次迁移已經運行了。

建立一个全新的项目,没有数据库

  1. 创建数据库
  2. 将向南添加到已安装的应用程序中
  3. 运行 syncdb,将 django 和 south 表添加到数据库中
  4. 添加你的应用程序
  5. 对于每个应用程序运行 python manage.py schemamigration app_name --initial,这将为您的应用程序创建初始迁移文件
  6. then run south migrate python manage.py migrate app_name this will add the tables to the database.

设置遗留项目和数据库

  1. 将向南添加到已安装的应用程序中
  2. 运行 syncdb,这将向数据库添加南表
  3. 对于每个应用程序运行 python manage.py schemamigration app_name --initial,这将创建您的初始迁移
  4. 对于每个运行 python manage.py migrate app_name 0001 --fake的应用程序来说,这将会假装是向南运行的,它不会对这些模型的数据库做任何事情,它只是将记录添加到 South _  歷史表中,以便下一次您想要建立迁移时,您已经准备好了。

设置遗留项目而不设置数据库

  1. 创建数据库
  2. 将向南添加到已安装的应用程序中
  3. 对于每个应用程序运行 python manage.py schemamigration app_name --initial,这将创建您的初始迁移
  4. 运行 syncdb,这将添加任何没有迁移到数据库的应用程序。
  5. 然后运行向南迁移 python manage.py migrate这将运行所有的应用程序迁移。

现在已经安装了 south,可以开始使用 south 来管理这些应用程序的模型更改。要运行的最常见命令是 python manage.py schemamigration app_name migration_name --auto,它将查看您运行的最后一次迁移,它将查找更改并为您构建一个迁移文件。然后你只需要运行 python manage.py migrate,它就会为你改变你的数据库。

希望能帮上忙。

我就是这样让事情运转起来的。

pip install South


# add 'south', to INSTALL_APPS, then
python manage.py syncdb


# For existing project + database
python manage.py convert_to_south app_name


# Thereafter, call them per model changes
python manage.py schemamigration app_name --auto
python manage.py migrate app_name

参考文献:

Http://garmoncheg.blogspot.com/2011/08/django-how-and-why-to-use-migrations.html Http://www.djangopro.com/2011/01/django-database-migration-tool-south-explained/

这似乎显而易见,但我强烈建议阅读文件。

即使在阅读了这个问题的答案之后,我仍然难以理解如何有效地使用 South。

当然,这一切在我阅读文档的那一天都改变了,你也应该这样做,南方比你想象的更容易使用。

Http://south.aeracode.org/docs/about.html

http://south.aeracode.org/docs/tutorial/index.html

Http://south.aeracode.org/docs/convertinganapp.html#converting-an-app

I also found this useful:

Http://www.djangopro.com/2011/01/django-database-migration-tool-south-explained/

And make sure you read Jeff Atwood's Coding Horror articles on database version control.

以防万一,如果南区给你找麻烦:

  1. 从应用程序目录中删除 migrations目录
  2. 从数据库中删除 南边 _ 迁移
  3. 运行 Py syncdb
  4. 返回到使用 South (例如’./management. py Convert _ to _ South something,./management. py shift...’)