Failed to load fixtures with south in Django project

I have a Django project that uses a South application to handle schema and data migration. In one of my applications, I have a migration (number 0004), which is responsible for loading data from a 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 am trying to add "soft delete" functionality, which requires the addition of another entry, defined as:

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

Based on this change, I added a new migration, which has the number 0009. After that, I run the migrate command, which gives me an 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'") 

This is rather strange, because this error occurs when applying the 0004 migration, which previously worked fine and from the Southern process point at this step, registered by deleted_at , should not and does not exist in my database. I found that moving the migration with the boot device from step 0004 after 0009 solves the problem, but it looks very dirty and not very suitable for solving this problem.

Do you have any tips on how I can solve this problem and handle migrations and sub-bindings correctly?

+1
source share
3 answers

I found a workaround to my problem. Finally, I removed the load devices from Southern migration and delegated this Fabric action. Now I have split the migration and downloaded the source data, so everything works as I expect.

0
source

I found a Django snippet that does the job!

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

Loads data according to the models frozen in the device, and not the actual definition of the model in the code of your application! Great for me.

+3
source

Another solution is to download the instrument file and paste it using the orm migration:

 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 will be bound to the current database structure.

0
source

Source: https://habr.com/ru/post/954731/


All Articles