7

I'm working on a django project where all my unit test cases were working perfectly.

Ass soon as I introduced a second database all my test cases that inherit from TestCase are broken. At this stage I haven't build any test case for that second database but my router is working fine.

When I run the tests I get the error,

"KeyError: 'SUPPORTS_TRANSACTIONS'"

It appears to me that is trying to check that that all the databases that I've got setup support transactions but the second database is never created.

Any ideas on how to have the test script to build the second database.

user298404
  • 203
  • 2
  • 9

5 Answers5

10

I realise this is quite an old thread, but I ran into it with the same issue, and my resolve was adding the multi_db = True flag to my testcase, e.g:

class TestThingWithMultipleDatabases(TestCase):
     multi_db = True

     def test_thing(self):
         pass

Source https://github.com/django/django/blob/master/django/test/testcases.py#L861

This causes django to call flush on all databases (or rollback if they support transactions)

I too am using a db router

I'm afraid I cant find this in Django's documentation, so no link for that

farridav
  • 933
  • 8
  • 18
  • 1
    Django docs: https://docs.djangoproject.com/en/2.0/topics/testing/tools/#multi-database-support – pymen Dec 22 '17 at 09:35
  • 5
    `multi_db` was deprecated since django 2.2, using [TestCase.databases](https://docs.djangoproject.com/en/2.2/topics/testing/tools/#multi-database-support) instead. – sih4sing5hog5 Apr 29 '21 at 01:38
4

yes I had a similar problem... my fix was to set 'SUPPORTS_TRANSACTIONS': True for each of the database connections in the settings file. Not sure if this is the correct way to fix it, but it worked for me.

Howard
  • 1,389
  • 11
  • 11
  • Works for me. Edited my settings/local.py, and added "'SUPPORTS_TRANSACTIONS': True," to the single 'default' entry inside my DATABASES dictionary. – Jonathan Hartley Jul 04 '11 at 19:46
2

Referring to that link Django doc Multi-Db you can:

from django.test import TransactionTestCase

class TestMyViews(TransactionTestCase):
    databases = {'default', 'other'} # {'__all__'} shold work too

    def test_index_page_view(self):
        call_some_test_code()

thanks to

@sih4sing5hog5

Peko Chan
  • 306
  • 5
  • 15
1

'SUPPORTS_TRANSACTIONS':True worked for me too. However I have a kind of weird multiple db setup using database routers. @user298404: how does your multiple db setup look like?

ps. sorry; not enough points for comment...

RickyA
  • 15,465
  • 5
  • 71
  • 95
0

Here is a multiple db setup that I currently have in production:

DATABASES = {
    # 'default' is used as the WRITE (master) connection
    DB_PRIMARY_MASTER: {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'main',
        'USER': 'main_write',
        'PASSWORD': 'XXXX',
        'HOST': 'db-master',
        'PORT': '3306',
        'SUPPORTS_TRANSACTIONS': True,
    },

    # Slave connections are READONLY
    DB_PRIMARY_SLAVE: {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'main',
        'USER': 'main_read',
        'PASSWORD': 'XXXX',
        'HOST': 'db-slave',
        'PORT': '3306',
        'TEST_MIRROR': DB_PRIMARY_MASTER,
        'SUPPORTS_TRANSACTIONS': True,
    },

    # 'mail_default' is used as the WRITE (master) connection for the mail database
    DB_MAIL_MASTER: {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dbmail',
        'USER': 'dbmail_write',
        'PASSWORD': 'XXXX',
        'HOST': 'db-mail-master',
        'PORT': '3306',
        'SUPPORTS_TRANSACTIONS': True,
    },

    # Slave connections are READONLY
    DB_MAIL_SLAVE: {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dbmail',
        'USER': 'dbmail_read',
        'PASSWORD': 'XXXX',
        'HOST': 'db-mail-slave',
        'PORT': '3306',
        'TEST_MIRROR': DB_MAIL_MASTER,
        'SUPPORTS_TRANSACTIONS': True,
    },
}

DB_PRIMARY_MASTER, DB_PRIMARY_SLAVE, DB_MAIL_MASTER, and DB_MAIL_SLAVE are all string constants so that they can be used in my database router.
Hint: DB_PRIMARY_MASTER='default'

I hope this helps!

Howard
  • 1,389
  • 11
  • 11