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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4840102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 17:45:56  来源:igfitidea点击:

Why don't my south migrations work?

pythondjangomigrationdjango-south

提问by TIMEX

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. Yes, it creates the south_migrationhistory table, but it also creates my app's tables.

但是当我运行 syncdb 时,Django 创建了一堆表。是的,它创建了 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? The only thing I can think of is remove my app from INSTALLED_APPS, then run syncdb, then add it back on.

如何让 South 正常工作并与所有内容正确同步?我唯一能想到的就是从 INSTALLED_APPS 中删除我的应用程序,然后运行 ​​syncdb,然后重新添加它。

That is SO SILLY.

那太傻了。

采纳答案by Ken Cochrane

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. The key is to know when to do what.

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.

你的第一个错误是当你删除你的迁移时,一旦你这样做了,然后运行了同步数据库,Django 不知道你想要南方再管理那个应用程序,所以它为你创建了表。当您创建初始迁移然后运行 ​​migrate 时,south 试图创建 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 wallThis will run the migration and create your tables.

  2. Fake out the initial migration run $ py manage.py migrate wall 0001 --fakeThis 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.

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

  2. 伪造初始迁移运行 $ 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
  4. add your apps
  5. for each app run python manage.py schemamigration app_name --initialthis will create the initial migration files for your app
  6. then run south migrate python manage.py migrate app_namethis will add the tables to the database.
  1. 创建你的数据库
  2. 将南添加到已安装的应用程序
  3. 运行syncdb,这会将django和south表添加到数据库中
  4. 添加您的应用
  5. 对于每个应用程序运行,python manage.py schemamigration app_name --initial这将为您的应用程序创建初始迁移文件
  6. 然后运行南迁移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
  3. for each of your apps run python manage.py schemamigration app_name --initialThis will create your initial migrations
  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.
  1. 将南添加到已安装的应用程序
  2. 运行syncdb,这会将南表添加到数据库中
  3. 对于您运行的每个应用程序python manage.py schemamigration app_name --initial这将创建您的初始迁移
  4. 对于您运行的每个应用程序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 --initialThis will create your initial migrations
  4. run syncdb, this will add any apps that don't have migrations to the database.
  5. then run south migrate python manage.py migratethis will run all migrations for your apps.
  1. 创建数据库
  2. 将南添加到已安装的应用程序
  3. 对于您运行的每个应用程序python manage.py schemamigration app_name --initial这将创建您的初始迁移
  4. 运行syncdb,这将添加任何没有迁移到数据库的应用程序。
  5. 然后运行南迁移python manage.py migrate这将为您的应用程序运行所有迁移。

Now that you are setup with south, you can start using south to manage model changes to those apps. The most common command to run is python manage.py schemamigration app_name migration_name --autothat will look at the last migration you ran and it will find the changes and build out a migration file for you. Then you just need to run python manage.py migrateand it alter your database for you.

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

Hope that helps.

希望有帮助。

回答by Andrew

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.)

(如果这失败了,抱怨 South_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 southto INSTALLED_APPS.
  2. Run syncdb.
  3. Thenfollow the tutorial.
  1. 添加到INSTALLED_APPS.
  2. 运行syncdb
  3. 然后按照教程操作。

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

即,syncdb在为新应用程序添加模型之前,您应该已经运行了。您从应用程序中删除应用程序的解决方案INSTALLED_APPS应该有效,但值得注意的是,这实际上只是一个“愚蠢”的解决方法,因为您之前错过了一步。已经syncdb被运行您创建该应用的模型前,你就不必了变通使用。

回答by Wolph

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.

如何让 South 正常工作并与所有内容正确同步?我唯一能想到的就是从 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. Than it works properly.

但主要问题是您的订单不正确。您应该在本教程之前运行过 syncdb。比它正常工作。

回答by CppLearner

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.htmlhttp://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 -南解释/

回答by Ashley Davis

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.

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

That all changed of course the day I read the docs and you should too, South is simpler to use than you might think.

当然,在我阅读文档的那一天,一切都改变了,您也应该这样做,South 的使用比您想象的要简单。

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 文章。

回答by Sevenearths

Just for future ref. If South is giving you any problems:

仅供将来参考。如果 South 给您带来任何问题:

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