Вопрос

I've Django project which is using South application to handle schema and data migration. In one of my applications I have migration (number 0004) which is responsible for loading data fixtures from json file:

class Migration(DataMigration):

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

In the same project I try to add functionality of 'soft delete' which needs adding one more filed, defined as:

deleted_at = models.DateTimeField(blank=True, null=True)

Based on this change I've added new migration, which has number 0009. After that I start migrate command which give me error:

DatabaseError: Problem installing fixture 'C:/Users/Grzegorz/PycharmProjects/Dummy Project/Dummy\app_subapp\fixtures\dummy_data.json': Could not load app_subapp.DummyData(pk=1): (1054, "Unknown column 'deleted_at' in 'field list'")

It's quite strange, because this error occurs while applying migration 0004 which earlier worked ok and from point of South process in this step filed deleted_at shouldn't and doesn't exists in my database.I've found that moving migration with loading fixture from step 0004 after 0009 resolves problem, but it looks like very dirty and not good approach to resolve this issue.

Do you have any advices how can I resolve this problem and properly handle migrations and fixture loading with South?

Это было полезно?

Решение 2

I've found workaround to my problem. Finally I've extracted load fixtures from South migration and delegated this action to Fabric. Now I have separated migration and load initial data, so everything works as I expect.

Другие советы

I found a Django snippet that does the job!

https://djangosnippets.org/snippets/2897/

It load the data according to the models frozen in the fixture rather the actual model definition in your apps code! Works perfect for me.

An other solution is to load the fixture file and insert it using the migration's orm :

from south.v2 import DataMigration
import json

class Migration(DataMigration):

    def forwards(self, orm):
        json_data=open("path/to/your/fixture.json")
        items = json.load(json_data)
        for item in items:
            # Be carefull, this lazy line won't resolve foreign keys
            obj = orm[item["model"]](**item["fields"])
            obj.save()

        json_data.close()

Using this method, you're fixture will be loaded within the current database structure.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top