2

Question:

How can I add multiple databases for testing in Django?

When I ran my test suits I got this Error:

AssertionError: Database queries to 'mig' are not allowed in this test. Add 'mig' to path_to_test_suit.MigrationServiceTest.databases to ensure proper test isolation and silence this failure.

Here are my PostgreSQL settings:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'db_1_name',
        'USER': 'db_1_user',
        'PASSWORD': 'db_1_passwd',
        'HOST': 'db_1_host',
        'PORT': 'db_1_port',
    },
    'mig': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': db_2_name,
        'USER': db_2_user,
        'PASSWORD': 'db_2_passwd',
        'HOST': 'db_2_host',
        'PORT': 'db_2_port',
    },
}

I use Django-nose for running test suits and use Django 2.2

2
  • dis you find a solution? Commented Jun 22, 2020 at 18:29
  • Hi, unfortunately, I forget the way that I fixed the problem. Commented Jun 24, 2020 at 1:55

2 Answers 2

3

You could use the database parameter in your tests. For example:

class TestYourClass(TestCase):
    databases = {'default', 'mig'}

def test_some_method(self):
    call_some_method()

For more information please see Multi-database support.

P.S. In earliest Django version than 2.2 please use multi_db = True

class TestYourClass(TestCase):
    multi_db = True
Sign up to request clarification or add additional context in comments.

Comments

-1

You need to grant DJANGO user the priviledge to write. Then, to make the tests and preserve your data, you have to make a copy of your schema with test_ as prefix and use the keywork --keepdb.

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.