#27288: In multi-db setup changes written to master are not seen on
read-replicas
-----------------------------------+-------------------------------
Reporter: Jose M Herrero | Owner: nobody
Type: Uncategorized | Status: new
Component: Testing framework | Version: 1.10
Severity: Normal | Keywords: multi-db dbrouter
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+-------------------------------
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'...
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27288>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/050.a629bbe361ddb9f5e3866313dd8982b7%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.