[Fixed]-Django – specify database for TestCase fixtures

5πŸ‘

There is actually a bug in Django that causes it to ignore the name-based db-specific pointers if you specify the entire fixture name.

so if you do fixtures = ["mydata.default.yaml", "mydata.myotherdatabase.yaml"]

It will load both fixtures into the default database.

But if you do fixtures = ['mydata']

It will load correctly. This is also true for dbengine specific filenames (e.g. mydata.default.postgresql.sql) as well.

πŸ‘€zenWeasel

2πŸ‘

Fixtures are targeted at specific databases by filename. This is true in TestCase instances as well, as they just call the loaddata command.

See https://docs.djangoproject.com/en/dev/ref/django-admin/#database-specific-fixtures

πŸ‘€GDorn

0πŸ‘

If you have a multi-db setup with models exclusive to each database. You need to save a fixture file for each database (with the non-applicable database files being empty).

If your code defines fixtures = ["sample"] and you have two databases default and other.

You need two files: sample.default.json and sample.other.json. If sample contains only models from db default, sample.other.json will be an empty file ([]) and vice-versa.

Tried with Django 3.2

πŸ‘€Kedar

Leave a comment