【转】django数据迁移

转自:http://tt4it.com/exchange/blog/discuss/214/

 

Migrations —— New in Django 1.7

  1. django.db.utils.ProgrammingError: (1146, "Table 'hongbaoyun.auth_user' doesn't exist")
  • migrate
    1. $ python manage.py migrate
    2. Operations to perform:
    3. Apply all migrations: admin, contenttypes, auth, sessions
    4. Running migrations:
    5. Rendering model states... DONE
    6. Applying contenttypes.0001_initial... OK
    7. Applying auth.0001_initial... OK
    8. Applying admin.0001_initial... OK
    9. Applying admin.0002_logentry_remove_auto_add... OK
    10. Applying contenttypes.0002_remove_content_type_name... OK
    11. Applying auth.0002_alter_permission_name_max_length... OK
    12. Applying auth.0003_alter_user_email_max_length... OK
    13. Applying auth.0004_alter_user_username_opts... OK
    14. Applying auth.0005_alter_user_last_login_null... OK
    15. Applying auth.0006_require_contenttypes_0002... OK
    16. Applying auth.0007_alter_validators_add_error_messages... OK
    17. Applying sessions.0001_initial... OK
  • createsuperuser
    1. python manage.py createsuperuser
  • makemigrations
    1. python manage.py makemigrations
  • migrate
    1. python manage.py migrate
  • forwards/backwards
    • 数据表、django_migrations 同步更改
      1. python manage.py migrate app_name 0001
    • 数据表不更改、django_migrations 更改
      1. python manage.py migrate app_name 0001 --fake
  • 问题列表
    • DEFAULT NULL
      1. `name` varchar(255) DEFAULT NULL,
      2. `nickname` varchar(255),
    • ValueError: Cannot serialize:

South —— Django 1.7 前

  • Django 的第三方 app South 就是专门做数据库表结构自动迁移工作,Jacob Kaplan-Moss 曾做过一次调查,South 名列最受欢迎的第三方 app。事实上,它现在已经俨然成为 Django 事实上的数据库表迁移标准,很多第三方 app 都会带 South migrations 脚本。
  • 初始化 APP 数据库
    1. python manage.py schemamigration app_name --initial
  • 增加列字段
    1. python manage.py schemamigration app_name --auto
  • 迁移到South
    • 对于一个已存在的项目(定义了 models,创建了相应的数据库,保存了响应的数据),这时想要使用 South 替代原来的 syncdb 只需要一些简单的步骤:
    • 同样需要现在 INSTALL_APP 里面添加 south,然后
      1. python manage.py syncdb # syncdb 已经被 South 更改,用来创建south_migrationhistory表
      2. python manage.py convert_to_south app_name # 在 app_name 目录下面创建 migrations 目录以及第一次迁移需要的 0001_initial.py 文件
    • 以后在这个项目中就可以正常使用 South 了
  • forwards/backwards
    • 数据表、south_migrationhistory 同步更改
      1. python manage.py migrate app_name 0001
    • 数据表不更改、south_migrationhistory 更改
      1. python manage.py migrate app_name 0001 --fake
  • 多分支冲突
    • 假设 app_name 从 0004 开始,master 和 branch2 两个分支各自开始开发
    • 等到 merge 的时候,master 到 0007,branch2 到 0008,并且两个分支没有添加相同的字段
    • 通过下述方式解决多分支合并
      • master 分支执行
        1. git merge branch2
      • 解决冲突
      • south_migrationhistory 回到 0004
        1. python manage.py migrate app_name 0004 --fake
      • 删除 0004 之后所有的 migrations
      • 生成新的 migration
        1. python manage.py schemamigration app_name --auto
      • south_migrationhistory 回到 0005
        1. python manage.py migrate app_name 0005 --fake
    • See Workflow for Using Django South with Multiple Code Branches
posted @ 2016-05-22 17:40  wswang  阅读(1590)  评论(0)    收藏  举报