Django 1.7 migrations won't recreate a dropped table, why?

前端 未结 10 1052
挽巷
挽巷 2020-12-23 14:16

Using Django 1.7 migrations.

I accidentally dropped a table in my database. I assumed that by running migration again this would recreate the table but no, Django st

10条回答
  •  臣服心动
    2020-12-23 14:29

    Full disclaimer, this is a destructive operation in some cases, and I mostly use it to remigrate parts of the system without affecting the DB.

    Have you tried doing it via the table django_migrations? Just remove the rows that map to the app label and the migration names in question and delete those rows.

    +----+-----------------------+----------------------------------------------------------+---------------------+
    | id | app                   | name                                                     | applied             |
    +----+-----------------------+----------------------------------------------------------+---------------------+
    |  1 | contenttypes          | 0001_initial                                             | 2015-03-07 16:32    |
    | 30 | homepage              | 0001_initial                                             | 2015-04-02 13:30:44 |
    | 31 | homepage              | 0002_auto_20150408_1751                                  | 2015-04-08 12:24:55 |
    | 32 | homepage              | 0003_remove_mappinghomepagemoduleinventory_inventoryinfo | 2015-04-09 08:09:59 |
    +----+-----------------------+----------------------------------------------------------+---------------------+
    

    So now if i want to remove homepage, I can just delete row 30, 31, 32.

    Of course since you dropped the tables too, you'd need to change django_content_type too:

    +----+----------------------------------------+-----------------------+--------------------------------------+
    | id | name                                   | app_label             | model                                |
    +----+----------------------------------------+-----------------------+--------------------------------------+
    |  1 | content type                           | contenttypes          | contenttype                          |
    |  2 | session                                | sessions              | session                              |
    |  3 | site                                   | sites                 | site                                 |
    | 92 | master_homepagemodule_extrafields      | homepage              | masterhomepagemoduleextrafields      |
    | 93 | mapping_homepagemodule_inventory       | homepage              | mappinghomepagemoduleinventory       |
    | 94 | master_homepagemodule_inventoryfields  | homepage              | masterhomepagemoduleinventoryfields  |
    | 95 | mapping_homepagemodule_inventoryfields | homepage              | mappinghomepagemoduleinventoryfields |
    | 96 | master_homepagemodule                  | homepage              | masterhomepagemodule                 |
    | 97 | mapping_homepagemodule_extrafields     | homepage              | mappinghomepagemoduleextrafields     |
    +----+----------------------------------------+-----------------------+--------------------------------------+
    

    So now you'd have to remove the tables that you need to remigrate need by dropping the rows for those tables.

    I've used this when time was scarce and we needed a quick dirty fix, or when playing around in development.
    Hope it helps you too!

提交回复
热议问题