为什么我的南迁不工作? [英] Why don't my south migrations work?

查看:133
本文介绍了为什么我的南迁不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我创建我的数据库。

First, I create my database.

create database mydb;

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

I add "south" to installed Apps. Then, I go to this tutorial: http://south.aeracode.org/docs/tutorial/part1.html

本教程告诉我这样做:

$ 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")

所以我用Google (这从来没有工作,因此我的870个问题在Stackoverflow问),我得到这个页面: http://groups.google.com/group/south-users/browse_thread/thread/d4c83f821dd2ca1c

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

但是当我运行syncdb时,Django会创建一堆表。是的,它创建了south_migrationhistory表,但是它也创建了我的应用程序的表。

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

然后我迁移:

$ py manage.py migrate wall

你知道吗?它给了我这个BS:

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.

如何让南方的工作和正确的一切都同步进行?我唯一可以想到的是从INSTALLED_APPS中删除我的应用程序,然后运行syncdb,然后将其重新添加。

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.

推荐答案

South允许您在首次启动新应用程序时创建迁移,并且尚未添加表数据库,以及为已经在数据库中拥有表的旧应用程序创建迁移。关键是要知道什么时候做什么。

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.

您的第一个错误是删除迁移时,一旦你这样做,然后运行syncdb,Django不知道你想要南方管理该应用程序所以它为你创建了表。当您创建初始迁移,然后运行迁移时,南方正在尝试创建django已创建的表,从而导致您的错误。

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. 从数据库中删除墙壁应用程序的表,然后运行 $ py manage.py migrate墙壁这将运行迁移并创建您的表。

  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 0001 --fake 这将告诉南方,你已经在数据库上有这些表,所以只是假的,这将在south_migrationhistory表中添加一行,以便下次运行迁移时,它会知道第一次迁移已经运行。

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.



设置品牌新项目和无数据库



Setting up a brand new project and no database


  1. 创建数据库

  2. 向南添加安装的应用程序

  3. 运行syncdb,这将把django和south表添加到th中e数据库

  4. 添加您的应用程序

  5. 为每个应用程序运行 python manage.py schemamigration app_name --initial 这将为您的应用程序创建初始迁移文件

  6. 然后运行south migrate python manage.py migrate app_name 这将添加

  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 --initial this will create the initial migration files for your app
  6. then run south migrate python manage.py migrate app_name this will add the tables to the database.



设置旧项目和数据库



Setting up a legacy project and database


  1. 向南添加安装的应用程序

  2. 运行syncdb,这会将南表添加到数据库

  3. 您的应用程序运行 python manage.py schemamigration app_name --initial 这将为您运行的每个应用程序创建您的初始迁移

  4. c $ c> python manage.py migrate app_name 0001 --fake ,这将会向南冒出,它不会对这些模型的数据库做任何事情,它只会将记录添加到south_migrationhistory表所以下次想要创建一个迁移时,你都设置好了。

  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 --initial This 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.



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



Setting up a legacy project and no database


  1. 创建数据库

  2. 向南添加安装的应用程序

  3. 为您的每个应用程序运行 python manage.py schemamigration app_name --initial 这将创建您的初始迁移

  4. 运行syncdb,这将添加任何没有迁移到数据库的应用程序。

  5. 然后运行南迁移 python manage.py migrate 这将运行您的应用程序的所有迁移。

  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
  4. run syncdb, this will add any apps that don't have migrations to the database.
  5. then run south migrate python manage.py migrate this will run all migrations for your apps.

现在,您可以使用南方设置,您可以开始使用南方管理这些应用程序的模型更改。最常见的运行命令是 python manage.py schemamigration app_name migration_name --auto ,它将查看您运行的最后一次迁移,它会发现更改并构建迁移档案给你那么你只需要运行 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 --auto that 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 migrate and it alter your database for you.

希望有帮助。

这篇关于为什么我的南迁不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆