Couldn't load fixtures with South in Django project
Asked Answered
I

3

1

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?

Incisure answered 9/9, 2013 at 13:3 Comment(0)
I
0

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.

Incisure answered 11/9, 2013 at 11:52 Comment(1)
If you never use default values on relations fields, it's ok, otherwise you have to deal with data migrations.Porterhouse
S
3

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.

Stereoscopic answered 24/3, 2014 at 12:19 Comment(2)
There is a typo in the snippet (get_mode -> get_model), otherwise it does the job ! (django 1.6).Porterhouse
Much appreciation for this!Shrieve
I
0

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.

Incisure answered 11/9, 2013 at 11:52 Comment(1)
If you never use default values on relations fields, it's ok, otherwise you have to deal with data migrations.Porterhouse
A
0

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.

Ammonium answered 7/2, 2014 at 15:38 Comment(1)
That's not good : no relation handling and bound to JSON format.Porterhouse

© 2022 - 2024 — McMap. All rights reserved.