<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>south &#8211; 天地一沙鸥</title>
	<atom:link href="https://haoluobo.com/tag/south/feed/" rel="self" type="application/rss+xml" />
	<link>https://haoluobo.com</link>
	<description>to be continue....</description>
	<lastBuildDate>Thu, 15 Dec 2011 15:52:39 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.2</generator>
	<item>
		<title>Django数据库迁移组件（South）</title>
		<link>https://haoluobo.com/2011/12/django-south/</link>
		
		<dc:creator><![CDATA[vicalloy]]></dc:creator>
		<pubDate>Thu, 15 Dec 2011 15:52:39 +0000</pubDate>
				<category><![CDATA[编程]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[south]]></category>
		<guid isPermaLink="false">/?p=10295</guid>

					<description><![CDATA[django提供syncdb命令，用于从models自动生成数据库。但在models结构变化后，syncdb并 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>django提供syncdb命令，用于从models自动生成数据库。但在models结构变化后，syncdb并无法自动实现数据库的更新。<a href="http://south.aeracode.org/" rel="nofollow">South<sup><img decoding="async" src="http://iot-kb.insigma.com.cn/images/icons/linkext7.gif" alt="" width="7" height="7" align="absmiddle" border="0" /></sup></a>组件即是为了解决该问题而出现的。<br />
下面简单介绍一下South的一些最常见用法，更详细的使用方法见South的官方手册。</p>
<p>假设我们创建了一个名叫southtut的app</p>
<ol>
<li>生成初始化数据库的south脚本。允许上述命令后将在对于的app目录下生成 <strong>migrations</strong>目录，south的数据库迁移脚本即保存在该目录。
<div>
<div>
<div>
<div id="highlighter_715347">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<div title="Hint: double-click to select code">
<div><code>./manage.py schemamigration southtut --initial</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</li>
<li>在所用south后，syncdb命令不会为使用south托管的app生成数据库。migrate命令用于执行south的数据库迁移脚本， 实现数据库更改。在这里执行的操作是为southtut创建相关的数据库表。不带app名字时，将对project中的所有app进行migrate操 作。
<div>
<div>
<div>
<div id="highlighter_169492">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<div title="Hint: double-click to select code">
<div><code>./manage.py migrate southtut</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</li>
<li>在很多应用场景中，我们已经用syncdb将数据库给创建好了。这时候运行上面的migrate命令将会提示相关表已经存在，命令执行失败。这 时候我们需要告诉south需要跳过某些migrate操作。上面的命令将告诉south，系统已经执行过了0001号数据库迁移脚本。
<div>
<div>
<div>
<div id="highlighter_881531">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<div title="Hint: double-click to select code">
<div><code>./manage.py migrate southtut </code><code>0001</code> <code>--fake</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</li>
<li>接下来我们对models进行了某些改动，修改后增减了某些字段。</li>
<li>该命令将自动生成数据库的迁移脚本。在migrations目录下可以看到新增加了文件0002_xxx.py，该文件即是此次models改 动的数据库升级脚本。打开文件后可看到其中有一个类Migration，类中有两个方法forwards和backwards。这两个方法分别实现数据库 升级和会滚时对数据库的操作，具体指令的含义参考south的数据库<a href="http://south.aeracode.org/docs/databaseapi.html" rel="nofollow">API<sup><img decoding="async" src="http://iot-kb.insigma.com.cn/images/icons/linkext7.gif" alt="" width="7" height="7" align="absmiddle" border="0" /></sup></a>。
<div>
<div>
<div>
<div id="highlighter_6912">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<div title="Hint: double-click to select code">
<div><code>./manage.py schemamigration southtut --auto</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</li>
<li>应用models的改动到数据库
<div>
<div>
<div>
<div id="highlighter_589483">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<div title="Hint: double-click to select code">
<div><code>./manage.py migrate southtut</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</li>
</ol>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
