Django renaming App and migrations

我的梦境 提交于 2019-12-18 04:28:08

问题


I have django app named "app1" with models and migrations files. I renamed this app to "app2" and I fixed all imports, urls ... But I have a problem with migrations files and data in tables. How can I write migrations with the correct way to ensure: - New installation => create the new tables - Update old versions => create new tables, move data, remove old tables

PS: there is several tables with many FK.

Here is my progress, I am not sure if I am on the good way: - all old migrations removed - makemigrations to generate new migrations files

After this 2 steps I can install my application, but still have problem with old version.

Question: what is the best way to migrate data?

PS: I don't use south.


回答1:


I found a solution that's works

1- fix old migrations with new FK and new app dependencies

2- force old migrations to create tables with old app name, so for that inn migrations.CreateModel.options, add 'db_table: 'app1_table_name'

3- in each migration file add replaces = [('app1', 'migration_file_name')]. this will tell to django that current migration (app2.migration_file_name) will replace the old file, this will prevenent django to execute migrations twice

4- create a migration file te rename tables, with migrations.AlterModelTable




回答2:


Renaming an app is always a tricky issue.

If you do the migration like a simple table renaming migration, at any moment the apps.get_model() for the old app cannot work because the app simply doesn't exist.

I found this answer. I know you are not using south, but I think might work the same way, just skip the south steps.

Basically, you have to:

  1. Dump the data, before rename, into a json file

  2. Run the script in the answer to rename references in the json file from the app1 to app2

  3. Rename app1 to app2 (all import references, settings.py, etc)

  4. Run the migrations to create the tables for app2

  5. Load the data from json file to the database

  6. Drop the app1 tables

I hope this help.




回答3:


This is elequently answered in this blog post.

The bullet points are:

  1. Rename the folder of the application you want to update.
  2. Update any and import statements to the folder you updated.
  3. Update entries for the django_content_type table to refer to the application's app_label.
  4. Update the table names of any models you haven't explicitly set the table name of. These table names inferred by the application name and need to be updated.
  5. Update entries for the django_migrations table and update the reference for each migration by setting the app field your new app label.
  6. Update any namespaced folder names that are within your /static or /templates folder. For example, you might have ./foo_app/templates/foo_app/index.html and it should be updated to ./bar_app/templates/bar_app/index.html.


来源:https://stackoverflow.com/questions/44957167/django-renaming-app-and-migrations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!