Django重命名App和迁移 [英] Django renaming App and migrations
问题描述
我有一个名为"app1"的Django应用,其中包含模型和迁移文件. 我将此应用程序重命名为"app2",并修复了所有导入,URL ... 但是我在迁移表中的文件和数据时遇到问题. 如何以正确的方式编写迁移,以确保: -新安装=>创建新表 -更新旧版本=>创建新表,移动数据,删除旧表
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:有几个表包含许多FK.
PS: there is several tables with many FK.
这是我的进步,我不确定自己是否处于良好状态: -删除了所有旧的迁移 -makemigrations生成新的迁移文件
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:我不使用南方.
PS: I don't use south.
推荐答案
我找到了可行的解决方案
I found a solution that's works
1-使用新的FK和新的应用依赖项修复旧的迁移
1- fix old migrations with new FK and new app dependencies
2-强制旧迁移创建具有旧应用名称的表,因此对于该inn迁移.CreateModel.options,添加'db_table:'app1_table_name'
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-在每个迁移文件中添加替换= [('app1','migration_file_name')].这将告诉django当前迁移(app2.migration_file_name)将替换旧文件,这将防止django两次执行迁移
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-使用migrations.AlterModelTable
4- create a migration file te rename tables, with migrations.AlterModelTable
这篇关于Django重命名App和迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!