﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
27288	In multi-db setup changes written to master are not seen on read-replicas	Jose M Herrero	nobody	"I'm triying to implement a multi-db platform with one master and some read-replicas, I wanted to reflect this structure also in tests. 
I'm following documentation at [https://docs.djangoproject.com/es/1.10/topics/db/multi-db/] and [https://docs.djangoproject.com/en/1.10/topics/testing/advanced/#tests-and-multiple-databases]
A simple test writing and reading back the object fails. (I have used a user with write permissions to avoid problem #27286)

To reproduce with nothing interfering I started a fresh project, these are the steps to reproduce:

1. Start a new Django 1.10.1 project with `django-admin startproject mysite` and cd into it `cd mysite`
2. Create a database router `mysite/dbrouter.py`
{{{
#!div style=""font-size: 80%""
Code highlighting:
  {{{#!python
class DbRouter(object):
    def db_for_read(self, model, **hints):
        return 'read_replica'

    def db_for_write(self, model, **hints):
        return 'default'

    def allow_relation(self, obj1, obj2, **hints):
        return True

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if db == 'read_replica':
            return False
        return True
   }}}
}}}

3. Edit `mysite.settings.py` with master and read databases and set the database router

{{{
#!div style=""font-size: 80%""
Code highlighting:
  {{{#!python
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mysite',
        'USER': 'myuser',
        'PASSWORD': 'mypassword',
        'HOST': '127.0.0.1',
        'PORT': '',
    },
    'read_replica': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mysite',
        'USER': 'myuser',
        'PASSWORD': 'mypassword',
        'HOST': '127.0.0.1',
        'PORT': '',
        'TEST': {
            'MIRROR': 'default',
        },

    },
}
DATABASE_ROUTERS = ['mysite.dbrouter.DbRouter']
}}}
}}}

4. Create `mysite/tests.py`
{{{
#!div style=""font-size: 80%""
Code highlighting:
  {{{#!python
from django.test import TestCase
from django.contrib.auth.models import User

class SurveyFormTest(TestCase):
    def setUp(self):
        self.user = User.objects.create_user('username')

    def test_sample(self):
        self.assertEqual(User.objects.count(), 1)
}}}
}}}
5. Execute the tests and bang! `python manage.py test mysite`

{{{
Creating test database for alias 'default'...
F
======================================================================
FAIL: test_sample (mysite.tests.SurveyFormTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""/home/chemary/workspace/myvirtualenv/mysite/mysite/tests.py"", line 9, in test_sample
    self.assertEqual(User.objects.count(), 1)
AssertionError: 0 != 1

----------------------------------------------------------------------
Ran 1 test in 0.111s

FAILED (failures=1)
Destroying test database for alias 'default'...
}}}
"	Uncategorized	closed	Testing framework	1.10	Normal	invalid	multi-db dbrouter		Unreviewed	0	0	0	0	0	0
