Domanda

So the issue is that I have a Django project running fine now.

I need to make two schema changes, A and B.

For some reason, I need to load some fixture to database after I apply migration A and then apply migration B.

I can do it manually, of course. Like:

./manage.py migrate my_app 0001
./manage.py loaddata my_fixture.json
./manage.py migrate my_app 0002

and that works fine.

However, in production, I want to deploy my project with script automatically. I don't want to add too many manual step in it.

My ideal solution is that I can automatically populate my fixture after my schema change (maybe with some special option parameters).

Does anyone have an idea how to do it?

PS: I may not giving enough info of my problem. So if you think problem itself is too vague, leave the comment, and let me see what I can do to make it more clear.

UPDATE: I have marked Serafeim as correct answer. Robert Jørgensgaard Engdahl points out a good point, which can be explained detailed in following post django loading data from fixture after backward migration / loaddata is using model schema not database schema

However, the problem we have is not exactly same. My schema A is to create a new table and won't change it in foreseeable future. migration B is to add a new column to another table which point to the table created by A. And that is why I need to pre-populate some data in new table (for some other complicate reason, I don't want to explain this too much). And I have tested the solution from Serafeim, it works.

HOWEVER, if anyone encounter a similar situation, look at the post shared by Robert Jørgensgaard Engdahl to understand the downside of this solution before you take it. Thanks again for both of answer providers!

È stato utile?

Soluzione

You should use south data migrations:

http://south.readthedocs.org/en/latest/tutorial/part3.html#data-migrations.

South already has a recipy for loading your data in http://south.readthedocs.org/en/latest/fixtures.html.

After you create migration 0001, create an empty migration that will be used to load your data with:

./manage.py datamigration appname load_myfixture

Then, in the 0002_load_myfixture.py migration module modify the forwards migration like this:

def forwards(self, orm):
    from django.core.management import call_command
    call_command("loaddata", "my_fixture.json")

You don't need to implement the backwards migration.

Altri suggerimenti

Introducing new rows in any migration should be considered harmful: It becomes impossible to "reset" the migrations; i.e. make a release with no ORM changes, but all migrations replaced with 0001_initial migrations. In long lived projects you want to keep this option open.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top