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_read',
'PASSWORD': 'myreadpassword',
'HOST': '127.0.0.1',
'PORT': '',
},
}
DATABASE_ROUTERS = ['mysite.dbrouter.DbRouter']
}}}
}}}
4. Create `mysite/tests.py`
{{{
#!div style="font-size: 80%"
Code highlighting:
{{{#!python
from django.test import TestCase
class SurveyFormTest(TestCase):
def test_sample(self):
print 'hello'
}}}
}}}
5. Execute the tests and bang! `python manage.py test mysite`
{{{
Creating test database for alias 'default'...
E
======================================================================
ERROR: setUpClass (mysite.tests.SurveyFormTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/chemary/workspace/myvirtualenv/local/lib/python2.7/site-
packages/django/test/testcases.py", line 1009, in setUpClass
if not connections_support_transactions():
File "/home/chemary/workspace/myvirtualenv/local/lib/python2.7/site-
packages/django/test/testcases.py", line 974, in
connections_support_transactions
for conn in connections.all())
File "/home/chemary/workspace/myvirtualenv/local/lib/python2.7/site-
packages/django/test/testcases.py", line 974, in <genexpr>
for conn in connections.all())
File "/home/chemary/workspace/myvirtualenv/local/lib/python2.7/site-
packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/chemary/workspace/myvirtualenv/local/lib/python2.7/site-
packages/django/db/backends/base/features.py", line 235, in
supports_transactions
cursor.execute('CREATE TABLE ROLLBACK_TEST (X INT)')
File "/home/chemary/workspace/myvirtualenv/local/lib/python2.7/site-
packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/chemary/workspace/myvirtualenv/local/lib/python2.7/site-
packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/chemary/workspace/myvirtualenv/local/lib/python2.7/site-
packages/django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
File "/home/chemary/workspace/myvirtualenv/local/lib/python2.7/site-
packages/django/db/backends/mysql/base.py", line 112, in execute
return self.cursor.execute(query, args)
File "/home/chemary/workspace/myvirtualenv/local/lib/python2.7/site-
packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/home/chemary/workspace/myvirtualenv/local/lib/python2.7/site-
packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
OperationalError: (1142, "CREATE command denied to user
'myuser_read'@'localhost' for table 'ROLLBACK_TEST'")
----------------------------------------------------------------------
Ran 0 tests in 0.026s
FAILED (errors=1)
Destroying test database for alias 'default'...
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27286>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Comment:
I have tried to add configuration mentioned here but the error is the same
[https://docs.djangoproject.com/en/1.10/topics/testing/advanced/#tests-
and-multiple-databases]
--
Ticket URL: <https://code.djangoproject.com/ticket/27286#comment:1>
Comment (by Tim Graham):
Do you have any ideas about how we could detect whether or not the
database supports transactions besides the
[https://github.com/django/django/blob/f2ff1b2fabbe26d5e61d690c4c5a47f9582f9300/django/db/backends/base/features.py#L238-L250
current algorithm] that requires creating a table?
--
Ticket URL: <https://code.djangoproject.com/ticket/27286#comment:2>
* owner: nobody => felixxm
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/27286#comment:3>
Comment (by felixxm):
New algorithm for mySQL that determines whether the database supports
transactions, instead of `CREATE TABLE...` test
[https://github.com/django/django/pull/7320 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/27286#comment:4>
* status: assigned => closed
* resolution: => duplicate
Comment:
Duplicate of #26541
--
Ticket URL: <https://code.djangoproject.com/ticket/27286#comment:5>