Django Fixtures minimal subset that respects foreign key constraints

51 Views Asked by At

I want to set up a test DB for my Django project in order to do some propert testing. Sadly our DB is huge and very interconnected. This means using the full DB for testing is very slow if django needs to import a fixture with millions of lines of data at the start. However creating a suitable subset of Data seems to be problematic too, because there are so many foreign key constraints.

I used this script to create a fixture:

from itertools import chain
from django.db.utils import DEFAULT_DB_ALIAS
from django.core import serializers
from django.contrib.admin.utils import NestedObjects

from models import EntryTable

collector = NestedObjects(using=DEFAULT_DB_ALIAS)  # database name
collector.collect([EntryTable.objects.get(pk=1)])

objects = list(chain.from_iterable(collector.data.values()))
objects.reverse()
with open("search/fixtures/TestSkeleton.json", "w") as f:
    f.write(serializers.serialize("json", objects))

However it seems that this does not resolve all dependencies. For example EntryTable is pointed to by a Table A, and A is pointed to by a table ALinkB, which also points to a table B. The relevant Objects in A and ALinkB are in the fixture however table B was not. Therefore loading this fixture resulted in violated foreign key constraints.

Models in this example:

from django.db import models
from django.db.models.fields.related import ForeignKey

class EntryTabel(models.Model):
    pass

class A(models.Model):
    et = models.ForeignKey(EntryTable)

class B(models.Model):
    pass

class ALinkB(models.Model):
    a = models.ForeignKey(A)
    b = models.ForeignKey(B)

Does someone know what I am missing?

0

There are 0 best solutions below