Question

I have a django app which consists of 17 models. Now I have realized that these models should be in 3 different apps(not in the original app). So now I would like to migrate these models out of the original app to these 3 different apps. How do I do that?

There exists foreign key, generic foreign key and ManyToMany relationships among the models. I also have data in the database(MySql), so I would like the data to be preserved during migration.

I have installed south for migrations, but don't know how to use it for solving this issue. I have gone through this similar question but could not find an answer that would solve my problem. Would be thankful for any help !

Was it helpful?

Solution

In my opinion, you have two ways of completing this task as stated below:

  1. Move the models and add Meta.db_table to refer the existing sql table as needed as @kroolik suggested
  2. Perform a three steps migration

The former is easier while the later could be better as tables would be named as you expect.

First of all, you mention you already has south installed. The first step would be to create the initial migration for the existing app. Take a look to the south tutorial. Then you must apply that migration, but as you already has the tables in db it would fail unless you include --fake flag.

After that you need to create the three apps you mention, and their models. Also create and apply (this time without fake flag) the initial migration for them.

Next step is write a datamigration. You must write it manually, although you can create the skeleton with datamigration. You must write "by hand" the migration.

Now you are almost done, the only remaining thing is remove the original tables. You can just remove those models, and create an "auto" schemamigration.

Don't forget to apply the migrations with migrate command. Also as @Bibhas mention a copy of database and/or a dump of it is a pretty good idea.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top