简体   繁体   English

为什么我的南迁不起作用?

[英]Why don't my south migrations work?

First, I create my database. 首先,我创建了我的数据库。

create database mydb;

I add "south" to installed Apps. 我在安装的应用程序中添加“南”。 Then, I go to this tutorial: http://south.aeracode.org/docs/tutorial/part1.html 然后,我转到本教程: 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 所以我使用谷歌(它从不起作用。因此我在Stackoverflow上询问了870个问题),我得到了这个页面: 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. 但是当我运行syncdb时,Django会创建一堆表。 Yes, it creates the south_migrationhistory table, but it also creates my app's tables. 是的,它创建了south_migrationhistory表,但它也创建了我的应用程序表。

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: 它给了我这个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? 如何让South工作并正确地与所有内容同步? The only thing I can think of is remove my app from INSTALLED_APPS, then run syncdb, then add it back on. 我唯一能想到的是从INSTALLED_APPS中删除我的应用程序,然后运行syncdb,然后重新添加它。

That is SO SILLY. 那就是S SILLY。

South allows you to create migrations when you first start out with a new app and the tables haven't been added to the database yet, as well as creating migrations for legacy apps that already have tables in the database. South允许您在刚开始使用新应用程序时创建迁移,并且尚未将表添加到数据库中,以及为已在数据库中具有表的旧应用程序创建迁移。 The key is to know when to do what. 关键是要知道何时做什么。

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. 你的第一个错误是当你删除你的迁移时,一旦你这样做了,然后运行了syncdb,Django就不知道你想让南方管理那个应用了,所以它为你创建了表。 When you created your initial migrations and then ran migrate, south was trying to create tables that django already created, and thus your error. 当您创建初始迁移然后运行迁移时,南方尝试创建django已创建的表,从而创建错误。

At this point you have two options. 此时您有两种选择。

  1. Delete the tables for the wall app from your database and then run $ py manage.py migrate wall This will run the migration and create your tables. 从数据库中删除墙上应用程序的表,然后运行$ py manage.py migrate wall这将运行迁移并创建表。

  2. Fake out the initial migration run $ py manage.py migrate wall 0001 --fake This will tell south that you already have the tables on the database so just fake it, which will add a row to the south_migrationhistory table, so that the next time you run a migrate it will know that the first migration has already been run. 假冒初始迁移运行$ py manage.py migrate wall 0001 --fake这将告诉南方你已经在数据库上有表,所以只是伪造它,这将在south_migrationhistory表中添加一行,以便下一次如果您运行迁移,它将知道第一次迁移已经运行。

Setting up a brand new project and no database 设置一个全新的项目,没有数据库

  1. create your database 创建数据库
  2. add south to installed apps 将南方添加到已安装的应用
  3. run syncdb, this will add the django and south tables to the database 运行syncdb,这会将django和南表添加到数据库中
  4. add your apps 添加你的应用
  5. for each app run python manage.py schemamigration app_name --initial this will create the initial migration files for your app 对于每个应用程序运行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. 然后运行南迁移python manage.py migrate app_name这将把表添加到数据库中。

Setting up a legacy project and database 设置旧项目和数据库

  1. add south to installed apps 将南方添加到已安装的应用
  2. run syncdb, this will add the south tables to the database 运行syncdb,这会将南表添加到数据库中
  3. for each of your apps run python manage.py schemamigration app_name --initial This will create your initial migrations 对于每个应用程序运行python manage.py schemamigration app_name --initial这将创建您的初始迁移
  4. for each of your apps run python manage.py migrate app_name 0001 --fake , this will fake out south, it won't do anything to the database for those models, it will just add records to the south_migrationhistory table so that the next time you want to create a migration, you are all set. 对于你的每个应用程序运行python manage.py migrate app_name 0001 --fake ,这将假冒南方,它不会对这些模型的数据库做任何事情,它只会将记录添加到south_migrationhistory表中,以便下一次你想创建一个迁移,你就完成了。

Setting up a legacy project and no database 设置遗留项目而没有数据库

  1. create database 创建数据库
  2. add south to installed apps 将南方添加到已安装的应用
  3. for each of your apps run python manage.py schemamigration app_name --initial This will create your initial migrations 对于每个应用程序运行python manage.py schemamigration app_name --initial这将创建您的初始迁移
  4. run syncdb, this will add any apps that don't have migrations to the database. 运行syncdb,这将添加任何没有迁移到数据库的应用程序。
  5. then run south migrate python manage.py migrate this will run all migrations for your apps. 然后运行南迁移python manage.py migrate这将运行您的应用程序的所有迁移。

Now that you are setup with south, you can start using south to manage model changes to those apps. 现在您已经设置了south,您可以开始使用south来管理对这些应用程序的模型更改。 The most common command to run is python manage.py schemamigration app_name migration_name --auto that will look at the last migration you ran and it will find the changes and build out a migration file for you. 最常见的命令是python manage.py schemamigration app_name migration_name --auto ,它将查看您上次运行的迁移,它将找到更改并为您构建迁移文件。 Then you just need to run python manage.py migrate and it alter your database for you. 然后你只需要运行python manage.py migrate并为你改变你的数据库。

Hope that helps. 希望有所帮助。

This is how I get things working. 这就是我让事情发挥作用的方式。

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

References: 参考文献:

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/ 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解释的/

The tutorial you're using states: 您正在使用的教程说明:

(If this fails complaining that south_migrationhistory does not exist, you forgot to run syncdb after you installed South .) (如果失败抱怨salt_migrationhistory不存在,则在安装South后忘记运行syncdb。)

Assuming that your post accurately details the steps you've taken, following that link seems to show that you missed a step before setting up your new app. 假设您的帖子准确地详细说明了您所采取的步骤,则该链接似乎表明您在设置新应用之前错过了一个步骤。 As you are following a tutorial for setting up migrations on a new application, the order is: 在您阅读有关在新应用程序上设置迁移的教程时,顺序为:

  1. Add south to INSTALLED_APPS . 向南添加到INSTALLED_APPS
  2. Run syncdb . 运行syncdb
  3. Then follow the tutorial. 然后按照教程。

Ie, you should've already run syncdb before you added in the models for your new app. 即,在添加新应用程序的模型之前,您应该已经运行了syncdb 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. 你从INSTALLED_APPS删除你的应用程序的解决方案应该可行,但是值得注意的是,它实际上只是一个“愚蠢”的解决方法,因为你错过了之前的一步。 Had syncdb been run before you created the models for that app, you wouldn't have to use the work-around. 如果在为该应用程序创建模型之前运行了syncdb ,则不必使用解决方法。

Just for future ref. 仅供将来参考。 If South is giving you any problems: 如果南方给你任何问题:

  1. Remove the migrations directories from your app directories 从应用程序目录中删除迁移目录
  2. Delete South _migrations from your database 从数据库中删除South _migrations
  3. Run manage.py syncdb 运行manage.py syncdb
  4. Go back to using South (eg './manage.py convert_to_south something, ./manage.py migrate ...') 回到使用South(例如'./manage.py convert_to_south something,。/ manage.py migrate ...')

This seems obvious, but I'd highly recommend reading the docs. 这似乎很明显,但我强烈建议阅读文档。

Even after reading the answers to this question I struggled to understand how to use South effectively. 即使在阅读了这个问题的答案后,我也很难理解如何有效地使用南方。

That all changed of course the day I read the docs and you should too, South is simpler to use than you might think. 当然,在我阅读文档的那一天,所有这些都发生了变化,你也应该这样做,南方比你想象的更容易使用。

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

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

http://south.aeracode.org/docs/convertinganapp.html#converting-an-app 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/ 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. 并确保您阅读了Jeff Atwood关于数据库版本控制的Coding Horror文章。

How do I get South to work and sync correctly with everything? 如何让South工作并正确地与所有内容同步? The only thing I can think of is remove my app from INSTALLED_APPS, then run syncdb, then add it back on. 我唯一能想到的是从INSTALLED_APPS中删除我的应用程序,然后运行syncdb,然后重新添加它。

I have used that fix with South troubles in the past. 我过去常常使用南方问题解决这个问题。 Not a pretty solution but very effective ;) 不是一个漂亮的解决方案但非常有效;)

But the main problem is that your order isn't correct. 但主要问题是您的订单不正确。 You should have run syncdb before the tutorial. 您应该在本教程之前运行syncdb。 Than it works properly. 比它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM