16

I have two databases that my site uses and I have an app that uses both of them. I need to write a TestCase that loads fixtures for both databases. I use a DB router, which works fine in production, but in the testing framework, Django insists on using the 'default' database for all fixtures, even for models that specify the other database. How do I tell Django to run a fixture against another database?

My TestCase is defined list:

class VerifierTestCase(TestCase):
    fixtures = ['zipcodes_test.json', 'all_states.json', 'wtf.json']
    multi_db = True
2
  • Has anybody found an answer for this? Cannot get fixtures to not all load into 'default' Commented Jul 14, 2011 at 18:56
  • Has anybody found an answer for this? Commented Oct 10, 2013 at 15:35

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately, this isn't true with Django 1.6. I get an error stating UserWarning: No fixture named 'migration_test_data' found when I try that.
@CoreDumpError, I also got the warning, but, fixtures were loaded correctly. I think the warnings are kind of bug. I issued a ticket about it: code.djangoproject.com/ticket/24680#ticket
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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.