Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent Django fixtures from conflicting with existing data

Tags:

python

django

I'm trying to do manage.py loaddata myfixture.json to purposely bulk overwrite several records in my database. However, it gives me the error:

IntegrityError: Problem installing fixture 'myfixture.json': Could not load myapp.Person(pk=1): (1062, "Duplicate entry 'Bob' for key 'name'")

I thought the behavior of loaddata was to overwrite the records if primary key matches an existing record? Am I wrong, or was this changed in Django 1.5? How do I get it to overwrite?

like image 809
Cerin Avatar asked May 30 '13 01:05

Cerin


People also ask

Does Django Loaddata overwrite?

Each time you run loaddata , the data will be read from the fixture and reloaded into the database. Note this means that if you change one of the rows created by a fixture and then run loaddata again, you'll wipe out any changes you've made.

How do I load all fixtures in Django?

By default, Django only loads fixtures into the default database. Use before_scenario to load the fixtures in all of the databases you have configured if your tests rely on the fixtures being loaded in all of them.


1 Answers

I found a way to modify Django's loaddata command to properly parse natural keys while loading, and avoid re-loading duplicates. The modified command is published here. The only differences between the official loaddata are lines 189-201.

like image 148
Cerin Avatar answered Sep 23 '22 11:09

Cerin