【django】解决migrations没有修改表的问题记录

问题情况

  • 在进行django开发的时候,遇到这样的情况,在models文件进行了新增表或修改表,删除了migrations,再生成新的migrations后进行migrate,会出现以下提示:
    1
    2
    3
    4
    5
    Operations to perform:
    Apply all migrations: admin, auth, contenttypes, interface, sessions, usermana
    ge
    Running migrations:
    No migrations to apply.

问题原理

  • 在同一个db库里,不同机器会发生这个问题;
  • 在django的db库里存在一张专门记录migrations的表”django_migrations”,通过查询可得出如下结果:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    mysql> select * from django_migrations;
    +----+--------------+------------------------------------------+----------------------------+
    | id | app | name | applied |
    +----+--------------+------------------------------------------+----------------------------+
    | 1 | interface | 0001_initial | 2017-03-07 09:34:26.118074 |
    | 2 | contenttypes | 0001_initial | 2017-03-07 09:34:26.148074 |
    | 3 | auth | 0001_initial | 2017-03-07 09:34:26.977426 |
    | 4 | admin | 0001_initial | 2017-03-07 09:34:27.126923 |
    | 5 | admin | 0002_logentry_remove_auto_add | 2017-03-07 09:34:27.167190 |
    | 6 | contenttypes | 0002_remove_content_type_name | 2017-03-07 09:34:27.261077 |
    | 7 | auth | 0002_alter_permission_name_max_length | 2017-03-07 09:34:27.306118 |
    | 8 | auth | 0003_alter_user_email_max_length | 2017-03-07 09:34:27.391483 |
    | 9 | auth | 0004_alter_user_username_opts | 2017-03-07 09:34:27.402682 |
    | 10 | auth | 0005_alter_user_last_login_null | 2017-03-07 09:34:27.446044 |
    | 11 | auth | 0006_require_contenttypes_0002 | 2017-03-07 09:34:27.450027 |
    | 12 | auth | 0007_alter_validators_add_error_messages | 2017-03-07 09:34:27.466104 |
    | 13 | auth | 0008_alter_user_username_max_length | 2017-03-07 09:34:27.514012 |
    | 14 | sessions | 0001_initial | 2017-03-07 09:34:27.589340 |
    | 15 | usermanage | 0001_initial | 2017-03-07 15:26:52.917539 |
    | 16 | usermanage | 0002_auto_20170320_1429 | 2017-03-20 14:30:02.230243 |
    +----+--------------+------------------------------------------+----------------------------+
    16 rows in set (0.00 sec)
  • 在表中id=1的记录中存在一个”interface 0001_initial”的操作记录,所以,当你删除所有migrations并新建”0001_initial.py”,执行migrate的时候就会出现上面的提示语;

  • 每次执行新的migrations时都会在该表中写入记录;

    解决方案

    在此记录该问题的解决方案:
  • 暴力解决:
    • 最简单快捷的方式就是将库整体删除掉,并新建新库再执行 0001_initial的migrate操作;
    • 如果db库里存在相当多的数据的时候强烈采用下面的方面,避免丢失数据!!!
  • 解决方案(一):

    • 进入到该django库后,删除id为1的记录;

      1
      delete from django_migrations where app='<your_app_name>';

      这里我们要删除的记录为:

      1
      delete from django_migrations where app='interface';
    • 删除该app名字下的migrations下的除了init.py之外的文件

    • 执行下面这两条命令:(在项目目录下)

      1
      2
      python manage.py makemigrations
      python manage.py migrate
    • 再查询表”django_migrations”:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      mysql> select * from django_migrations;
      +----+--------------+------------------------------------------+----------------------------+
      | id | app | name | applied |
      +----+--------------+------------------------------------------+----------------------------+
      | 2 | contenttypes | 0001_initial | 2017-03-07 09:34:26.148074 |
      | 3 | auth | 0001_initial | 2017-03-07 09:34:26.977426 |
      | 4 | admin | 0001_initial | 2017-03-07 09:34:27.126923 |
      | 5 | admin | 0002_logentry_remove_auto_add | 2017-03-07 09:34:27.167190 |
      | 6 | contenttypes | 0002_remove_content_type_name | 2017-03-07 09:34:27.261077 |
      | 7 | auth | 0002_alter_permission_name_max_length | 2017-03-07 09:34:27.306118 |
      | 8 | auth | 0003_alter_user_email_max_length | 2017-03-07 09:34:27.391483 |
      | 9 | auth | 0004_alter_user_username_opts | 2017-03-07 09:34:27.402682 |
      | 10 | auth | 0005_alter_user_last_login_null | 2017-03-07 09:34:27.446044 |
      | 11 | auth | 0006_require_contenttypes_0002 | 2017-03-07 09:34:27.450027 |
      | 12 | auth | 0007_alter_validators_add_error_messages | 2017-03-07 09:34:27.466104 |
      | 13 | auth | 0008_alter_user_username_max_length | 2017-03-07 09:34:27.514012 |
      | 14 | sessions | 0001_initial | 2017-03-07 09:34:27.589340 |
      | 15 | usermanage | 0001_initial | 2017-03-07 15:26:52.917539 |
      | 16 | usermanage | 0002_auto_20170320_1429 | 2017-03-20 14:30:02.230243 |
      | 17 | interface | 0001_initial | 2017-04-10 19:28:41.710626 |
      +----+--------------+------------------------------------------+----------------------------+
      16 rows in set (0.00 sec)
    • 可发现已经新增一条”interface 0001_initial”的操作记录;

  • 解决方案(二):
    • 已知”每次执行新的migrations时都会在该表中写入记录;”
    • 在不删除表记录情况下,修改migrations的序号,如表中只有”0001_initial”的记录,则将本地目录的migration更改为”0002_initial.py” 后缀可自定义;
    • 执行该重命名序号的migrations文件即可;