syncdb 仅仅能初始化table(create),不能自己主动update/delete/drop。

那么south应运而生。

south简单使用方法:

安装:

pip install South

安装完后,增加到INSTALLED_APPS中,并syncdb一下,否则会出现south_migrationhistory 表找不到。

(注:此时syncdb的行为被south改变了,注意命令行输出,表south_migrationhistory的结构例如以下:

測试安装:

manage.py shell

>> import south

建立一个test_south的app,并建立模型south_test,加入一个字段name。 将app加入到INSTALLED_APPS中。

from django.db import models

class south_test(models.Model):
name = models.CharField(max_length=100)
# Create your models here.

第一次使用:

manage.py  schemamigration your_app --initial

manage.py migrate your_app

本例中,your_app自然是test_south,以下是发生了神马(注意黄色加亮部分):

这一步,仅仅是生成了用于创建数据库表的迁移文件,主要是0001_initial.py,内容例如以下:

这一步,才真正生成了数据库模型,而且在south_migrationhistory加入了一条记录。

数据表south_migrationhistory 插入了一条记录,例如以下:

兴许自己主动ORM:

manage.py  schemamigration your_app --auto

manage.py migrate your_app

为了演示,如果如今,因为需求变化,我们改动模型test_south.south_test,加入字段 memo,例如以下:

from django.db import models

class south_test(models.Model):
name = models.CharField(max_length=100)
memo = models.CharField(max_length=100)

我们不再运行syncdb,改为运行schemamigration / migrate, 例如以下:

创建了一个0002的文件,例如以下:

migrate之前,我们看下south_test表

运行migrate,例如以下:

再次查看数据库表south_test

我们再次观察下south_migrationhistory表,例如以下:

==========================

对于已经存在的app,增加south非常easy,命令例如以下:

manage.py convert_to_south your_app

它仅仅是在表south_migrationhistory里面加入一条记录(同一时候生成迁移文件)。

如果我们这里的your_app是books,那么south_migrationhistory的记录例如以下:

(注意:假设代码增加了版本号控制---比方svn,那么其它人下载代码后,须要运行manage.py migrate myapp 0001 --fake,由于convert_to_south总是会尝试创建全部已经存在了的表,通过这个命令告诉south,myapp已经创建了这些表,它不须要再次创建了。以后就依照正常的迁移来做:

manage.py syncdb

后面假设有改动模型,则运行

manage.py  shemamigration
myapp --auto

manage.py migrate
myapp

很多其它信息,能够參考这里:http://south.readthedocs.org/en/latest/tutorial/part1.html#tutorial-part-1

转载请注明来自:http://blog.csdn.net/lion_awake/article/details/38283621,谢谢!

django schema migration的更多相关文章

  1. django中migration文件是干啥的

    昨天很蠢的问leader git push的时候会不会把本地的数据库文件上传上去,意思是django中那些migration文件修改之后会不会上传. 然后得知不会,因为所有的数据库都存在本机的mysq ...

  2. django清理migration终极解决办法

    1.django生成数据表结构的过程 在我们设计好models以后,我们可以通过以下命令生成将要同步给数据库的数据结构文件 python manage.py makemigrations 生成的文件在 ...

  3. sublime text 3插件

    Package Control Messages Emmet emmet插件 Thank you for installing Emmet -- a toolkit that can greatly ...

  4. GitHub 开源的 MySQL 在线更改 Schema 工具【转】

    本文来自:https://segmentfault.com/a/1190000006158503 原文:gh-ost: GitHub's online schema migration tool fo ...

  5. django使用migrations迁移版本和数据库中报错解决方案

    1.到数据库表django_migrations中查看app中看看app列 2.到项目对应的app模块中打开migrations文件查看生成的文件与数据库app列中的是不是一样 3.找到哪里不一致的文 ...

  6. Django WEB开发 - Django 3.0 Tutorial入门

    1. Django 官方网站 https://docs.djangoproject.com/zh-hans/3.0/intro/tutorial01/ 2. Model View Controller ...

  7. Go 语言相关的优秀框架,库及软件列表

    If you see a package or project here that is no longer maintained or is not a good fit, please submi ...

  8. Awesome Go精选的Go框架,库和软件的精选清单.A curated list of awesome Go frameworks, libraries and software

    Awesome Go      financial support to Awesome Go A curated list of awesome Go frameworks, libraries a ...

  9. iOS面试必看

    转载:http://www.jianshu.com/p/5d2163640e26 序言 目前形势,参加到iOS队伍的人是越来越多,甚至已经到供过于求了.今年,找过工作人可能会更深刻地体会到今年的就业形 ...

随机推荐

  1. Python 时间整理

    在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块. 在开始之前,首先要说明这几点: ...

  2. arm tiny6410双网卡桥接问题

    这几天做实验,想用arm tiny6410板子做个mesh网络节点.该板子本身自带一个lan网卡,我自己配了一个tp-link的usb无线网卡.其中wlan网卡工作在adhoc模式下,作为mesh骨干 ...

  3. IE浏览器 json异常

    当使用json数据结构时,如果对象数组最后一个元素后面依然跟一个“,”,在非IE浏览器下运行正常,但是,在IE浏览器上,则会报错. 如果使用for循环遍历对象数组时,由于后面多了一个分割符" ...

  4. SpriteKit

    [SpriteKit] Sprite Kit provides a graphics rendering and animation infrastructure that you can use t ...

  5. utf8 和 UTF-8 的区别

    只有在MySQL中可以使用“utf-8”的别名“utf8”,但是在其他地方一律使用大写“UTF-8”.

  6. RAID对硬盘的要求及其相关

    Raid 0:至少需要两块硬盘,磁盘越多,读写速度越快,没有冗余. Raid 1:只能用两块硬盘,两块硬盘的数据互为镜像(写慢,读快),一块磁盘冗余. Raid 5:至少需要3块硬盘,一块磁盘冗余. ...

  7. GridView九宫图

    了解下 <!--android:numColumns="auto_fit"--列数设置为自动: android:columnWidth="90dp"--每 ...

  8. (博弈论)hdoj 1525 Euclid's Game

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1525 题目大意:Stan和Ollie在玩一个游戏,a和b两个数字,每次可以选择较大的数减去较小的数的若 ...

  9. Apache服务器配置默认首页文件名和网站路径

    默认首页的配置: 第一种:直接修改apache服务器的配置文件./conf/httpd.conf中的DirectoryIndex,如:(项目web以index.php为首页) <IfModule ...

  10. Eclipse 和 NetBeans 快捷键即其他常用功能比较

    按: 自己用 Eclipse, 常用的也就这些功能, 在用 NetBeans 时, 有些不顺手, 因此列表如下. Eclipse和NetBeans常用快捷键对比:  功能  Eclipse     N ...