PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, April 13, 2022

[FIXED] How to rename a Django app and migrate data from an app to the other

 April 13, 2022     database-migration, django, migration     No comments   

Issue

I have a Django app named app1 with models and migrations files. I renamed this app to app2 and I fixed all imports, urls etc... I now 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

Note 1: there is several tables with many Foreign Keys.

Here is my progress so far and I am not sure if I am on the good way:

  • I removed all older migrations
  • I ran python manage.py makemigrations to generate new migrations files

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

Question: What is the best way to migrate data?

Note 2: I don't use South.


Solution

I found a solution that's works

  1. Fix old migrations with new Foreign Keys and new app dependencies.
  2. Force old migrations to create tables with old app name, so for that in 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 to rename tables with migrations.AlterModelTable


Answered By - Touhami
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing