{"id":2746,"date":"2013-05-10T10:01:09","date_gmt":"2013-05-10T02:01:09","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=2746"},"modified":"2013-05-10T10:01:09","modified_gmt":"2013-05-10T02:01:09","slug":"%e4%bd%bf%e7%94%a8django-south%e5%81%9amigration","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/2746.html","title":{"rendered":"\u4f7f\u7528Django South\u505aMigration"},"content":{"rendered":"<p><span style=\"font-size: small;\">\u4f7f\u7528South\u4e4b\u524d\u94ed\u8bb0\uff1a\u8bf7\u4f60\u4e00\u5b9a\u8981\u76f8\u4fe1\u4ed6\u7684\u80fd\u529b\uff0c\u629b\u5f03\u5bf9\u4ed6\u7684\u4e0d\u4fe1\u4efb\u611f\u3002\u56e0\u4e3aSouth\u7ed9\u4eba\u7684\u7b2c\u4e00\u5370\u8c61\u5c31\u662f\u597d\u50cf\u6bcf\u4e2a\u64cd\u4f5c\u90fd\u5728\u629b\u5f02\u5e38\u3002<\/span><\/p>\n<p><span style=\"font-size: small; color: #ff0000;\">South\u6982\u8ff0<\/span><\/p>\n<p><span style=\"font-size: small;\">\u9488\u5bf9django\u81ea\u5e26\u7684syncdb\u540c\u6b65models\u548c\u6570\u636e\u5e93\u7684\u7f3a\u9677\u5f00\u53d1\u7684\u6570\u636e\u8fc1\u79fb\u5de5\u5177\uff0c\u53ef\u4ee5\u4f5c\u4e3asyncdb\u7684\u66ff\u4ee3\uff0cSouth\u80fd\u591f\u68c0\u6d4b\u5bf9models\u7684\u66f4\u6539\u5e76\u540c\u6b65\u5230\u6570\u636e\u5e93.<\/span><\/p>\n<p><span style=\"font-size: small; color: #ff0000;\">South\u57fa\u672c\u7528\u6cd5<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5b89\u88c5\u5b8cSouth\u4e4b\u540e\uff0c\u8981\u5728django\u9879\u76ee\u4e2d\u4f7f\u7528South\uff0c\u5148\u8981\u5c06South\u4f5c\u4e3a\u4e00\u4e2aApp\u5bfc\u5165\u9879\u76ee\uff0c\u6240\u4ee5\u8bbe\u7f6eINSTALL_APP\u6dfb\u52a0south<\/span><br \/>\n<span style=\"font-size: small;\"> \u7b2c\u4e00\u6b21\u4f7f\u7528South\uff08\u5bf9\u4e8e\u5df2\u5b58\u5728\u7684\u9879\u76ee\u8f6c\u7528South\u89c1\u4e0b\u4e00\u6b65\u7684\u4ecb\u7ecd\uff09<\/span><\/p>\n<p><span style=\"font-size: small;\">Python\u4ee3\u7801<\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\npython manage.py schemamigration youappname --initial\r\n# --initial\u5728\u6570\u636e\u5e93\u521b\u5efamodels\u5b9a\u4e49\u7684\u8868\uff0c\u4ee5\u53caSouth\u9700\u8981\u7684south_migrationhistory\u8868\uff0c\u53e6\u5916\u4f1a\u5728youappname\u76ee\u5f55\u4e0b\u9762\u521b\u5efa\u4e00\u4e2a migrations \u7684\u5b50\u76ee\u5f55\r\n#\u4ee5\u540e\u6bcf\u6b21\u5bf9models\u66f4\u6539\u540e\uff0c\u53ef\u4ee5\u8fd0\u884c\u4ee5\u4e0b\u4e24\u6761\u547d\u4ee4\u540c\u6b65\u5230\u6570\u636e\u5e93\r\n\r\npython manage.py schemamigration youappname --auto     #\u68c0\u6d4b\u5bf9models\u7684\u66f4\u6539\uff0c\u751f\u6210\u5bf9\u5e94\u7684\u6587\u4ef6\r\npython manage.py migrate youappnam  #\u5c06\u66f4\u6539\u53cd\u5e94\u5230\u6570\u636e\u5e93\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u6bcf\u6b21\u8fdb\u884cmigrate\u64cd\u4f5c\uff0csouth\u90fd\u4f1a\u53bb south_migrationhistory \u8868\u67e5\u770b\u8bb0\u5f55\uff0c\u770bmigrations\u76ee\u5f55\u4e0b\u9762\u54ea\u4e9b\u6587\u4ef6\u6ca1\u6709\u88ab\u6267\u884c\uff0c\u7136\u540e\u6267\u884c\u3002<\/span><\/p>\n<p><span style=\"font-size: small; color: #ff0000;\">\u8fc1\u79fb\u5230South<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5bf9\u4e8e\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u9879\u76ee\uff08\u5b9a\u4e49\u4e86models\uff0c\u521b\u5efa\u4e86\u76f8\u5e94\u7684\u6570\u636e\u5e93\uff0c\u4fdd\u5b58\u4e86\u54cd\u5e94\u7684\u6570\u636e\uff09\uff0c\u8fd9\u65f6\u60f3\u8981\u4f7f\u7528South\u66ff\u4ee3\u539f\u6765\u7684syncdb\u53ea\u9700\u8981\u4e00\u4e9b\u7b80\u5355\u7684\u6b65\u9aa4\uff1a<\/span><br \/>\n<span style=\"font-size: small;\"> \u540c\u6837\u9700\u8981\u73b0\u5728INSTALL_APP\u91cc\u9762\u6dfb\u52a0south\uff0c\u7136\u540e python manage.py syncdb #syncdb\u5df2\u7ecf\u88abSouth\u66f4\u6539\uff0c\u7528\u6765\u521b\u5efasouth_migrationhistory\u8868<\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\npython manage.py syncdb  #syncdb\u5df2\u7ecf\u88abSouth\u66f4\u6539\uff0c\u7528\u6765\u521b\u5efasouth_migrationhistory\u8868\r\npython manage.py convert_to_south youappname #\u5728youappname\u76ee\u5f55\u4e0b\u9762\u521b\u5efamigrations\u76ee\u5f55\u4ee5\u53ca\u7b2c\u4e00\u6b21\u8fc1\u79fb\u9700\u8981\u7684\u6570\u636e\u5e93\u7b49\r\n<\/pre>\n<p><span style=\"font-size: small; color: #ff0000;\">South\u540c\u6b65\u539f\u7406<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5bf9\u5e94\u6bcf\u6b21 models\u7684\u66f4\u6539\u6267\u884cschemamigration\u540e\u4f1a\u5728migrations\u76ee\u5f55\u4e0b\u9762\u751f\u6210\u5bf9\u5e94\u6b64\u6b21\u66f4\u6539\u7684py\u6587\u4ef6\uff08South\u79f0\u4e4b\u4e3a migrate\uff09\uff0c\u6587\u4ef6\u540d\u5f62\u5982 0002_autodel_field_notes_create_user.py\uff0c\u540c\u6b65\u6570\u636e\u5e93\u7684\u65f6\u5019\u4f1a\u987a\u5e8f\uff08\u6587\u4ef6\u540d ASCII\u6392\u5e8f\uff09\u6267\u884c\u8fd9\u4e9bpy\u6587\u4ef6\uff0c\u6587\u4ef6\u91cc\u5305\u542b\u4e00\u4e2aMigration\u7684\u7c7b\uff0c\u91cc\u9762\u6709\u4e24\u4e2a\u65b9\u6cd5forwards\u548cbackwards\uff0c\u5c06\u66f4\u6539\u540c\u6b65\u5230\u6570\u636e\u5e93\u4f1a \u6267\u884cforwards\u65b9\u6cd5\uff0c\u6570\u636e\u5e93\u64cd\u4f5c\u5931\u8d25\u4f1a\u8c03\u7528backwards\u5b9e\u73b0rollback\uff0cSouth\u8fd8\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u56de\u6eaf\u7684\u529f\u80fd\u3002<\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#You can also specify a specific migration to migrate:\r\npython manage.py migrate 0002_autodel_field_notes_create_user.py\r\n#Note that, if the system has already migrated past the specified migration,\r\n#it will roll back to it instead. If you want to migrate all the way back,\r\n#specify the special migration name zero:\r\npython manage.py migrate zero\r\n<\/pre>\n<p><span style=\"font-size: small; color: #ff0000;\">\u5e38\u89c1\u95ee\u9898<\/span><\/p>\n<p><span style=\"font-size: small;\">1. \u6dfb\u52a0\u548c\u5220\u9664\u5b57\u6bb5\u65f6\u53ef\u80fd\u4f1a\u8981\u6c42\u8f93\u5165 default value\uff08django\u91cc\u9762models\u91cc\u9762\u7684\u8bb8\u591a\u5b57\u6bb5\u9ed8\u8ba4\u90fd\u662fnull=False\uff09<\/span><br \/>\n<span style=\"font-size: small;\"> \u5bf9\u4e8e\u6dfb\u52a0\u5b57\u6bb5\uff0c\u8f93\u5165\u7684\u9ed8\u8ba4\u503c\u5fc5\u987b\u548cmodels\u5b9a\u4e49\u7684\u7c7b\u578b\u5339\u914d\uff0c\u5426\u5219\u540c\u6b65\u6570\u636e\u5e93\u7684\u65f6\u5019\u4f1a\u62a5\u9519<\/span><br \/>\n<span style=\"font-size: small;\"> \u5bf9\u4e8e\u5220\u9664\u5b57\u6bb5\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u968f\u610f\u8f93\u5165\u4e00\u4e2avalue\u800c\u4e0d\u7ba1\u5b57\u6bb5\u7684\u9ed8\u8ba4\u7c7b\u578b\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5220\u9664\uff0c\u4f46\u662f\u5e76\u4e0d\u53ef\u53d6\uff0c\u5177\u4f53\u539f\u56e0\u53ef\u4ee5\u53c2\u89c1South\u7684\u5b9e\u73b0\u673a\u5236(rollback\u5230\u5220\u9664\u5b57\u6bb5\u4e4b\u524d\u4f1a\u5931\u8d25\uff09\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">2. \u5982\u679cSouth\u5728\u540c\u6b65\u6570\u636e\u5e93\u7684\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u9519\u8bef\uff0c\u5219migrations\u76ee\u5f55\u4e0b\u9762\u5bf9\u5e94\u6b64\u6b21\u66f4\u6539\u7684python\u6587\u4ef6\u4e0d\u4f1a\u88ab\u6267\u884c\uff0c\u53ef\u4ee5\u8fd0\u884cpython manage.py migrate &#8211;list\u67e5\u770b\u6ca1\u6709\u6267\u884c\u7684py\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u524d\u9762\u6ca1\u6709*\u8868\u793a\u8be5\u6587\u4ef6\u5bf9\u5e94\u7684\u66f4\u6539\u6ca1\u6709\u53cd\u5e94\u5230\u6570\u636e\u5e93\uff0c\u53ea\u9700\u5220\u9664\u6389\u8fd9\u4e9b\u6709\u95ee\u9898\u7684migrate\uff0c\u53c2\u7167\u9519\u8bef\u63d0\u793a\u4fee\u6539 models\u518d\u540c\u6b65\u5373\u53ef\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u66f4\u6539\u5bf9\u5e94\u7684py\u6587\u4ef6\u4fee\u590d\u9519\u8bef\uff0c\u7136\u540e\u624b\u52a8\u628a\u6267\u884c\u6210\u529f\u7684\u90a3\u90e8\u5206\u6539\u56de\u6765\uff0c\u6216\u8005\u628a\u90a3\u90e8\u5206\u4ee3\u7801\u6ce8\u91ca\u6389\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5f3a\u70c8\u5efa\u8bae\u628asouth\u7684\u5b98\u65b9\u6587\u6863\u901a\u770b\u4e00\u904d\uff0c\u6587\u6863\u5199\u5f88\u8be6\u7ec6\u3002<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528South\u4e4b\u524d\u94ed\u8bb0\uff1a\u8bf7\u4f60\u4e00\u5b9a\u8981\u76f8\u4fe1\u4ed6\u7684\u80fd\u529b\uff0c\u629b\u5f03\u5bf9\u4ed6\u7684\u4e0d\u4fe1\u4efb\u611f\u3002\u56e0\u4e3aSouth\u7ed9\u4eba\u7684\u7b2c\u4e00\u5370\u8c61\u5c31\u662f\u597d\u50cf\u6bcf\u4e2a\u64cd [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-2746","post","type-post","status-publish","format-standard","hentry","category-skill"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2746","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=2746"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2746\/revisions"}],"predecessor-version":[{"id":2747,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2746\/revisions\/2747"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=2746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=2746"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=2746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}