Test database issue when multiple database

23 views
Skip to first unread message

Sylvain Duquenne

unread,
Jan 13, 2017, 7:27:40 AM1/13/17
to Django users
Hi everyone,

I found a weird behaviour when I run my Django tests, it doesn't always use the test database, sometimes it uses the production database and truncate it!

I looked at the Django source code and the cause of this behaviour seems to be in  'django/test/utils.py' in 'setup_databases()' where a test database is created (line 183) only for the first alias, the other connections are set as mirrors of the first one (line 198). 

The connection mirroring is done in 'django/db/backends/base/creation.py' in 'set_as_test_mirror()' where only the name in the settings dictionary is changed, the connection is not reinitialised which causes the connection to still point on the production database, adding 'self.connection.close()'  before the settings dictionary affectation fixes the issue.

Did any of you has experienced this issue before?

Let me know what you think about it before I create a bug report.

Thanks in advance,

Sylvain
Reply all
Reply to author
Forward
0 new messages