﻿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
14610	fixtures should be able to specify their database	cyberlogi	nobody	"I have a product that has two databases, one is the member related database and the other is a 10gb static database. For performance reasons, I have to keep them separate and I have a database router which handles this. For testing purposes I need to load both databases for some of my function and view tests. The initial load of the test databases works correctly and I have two separate test databases, but when I specify fixtures in my TestCase, they all run against the default database, which causes the fixtures to fail to load.

After spending the last 24 hours diving through the Django source code and talking to other developers, I cannot find a way to specify the database on which a fixture should load (when the 'syncdb' command is executed). This seems like a feature that should be build into fixtures (or the TestCase framework), otherwise developers can't test code that runs against multiple databases.

My database setup is:
{{{
DATABASES = {
	'default': {
		'NAME': 'matt',
		'ENGINE': 'mysql',
		'USER': 'myuser',
		'PASSWORD': 'changeit',
		'HOST': ''
	},
	'voters': {
		'NAME': 'voters',
		'ENGINE': 'mysql',
		'USER': 'myuser',
		'PASSWORD': 'changeit',
		'HOST': ''
	}
}
}}}

Here is my simple router:

{{{
from django.conf import settings

class VotizenRouter(object):
	""""""A router to control all database operations on models in
		the myapp application""""""

	def db_for_read(self, model, **hints):
		""Point all operations on myapp models to 'other'""
		if model._meta.app_label == 'verifier':
			return 'voters'
		return None

	def db_for_write(self, model, **hints):
		""Point all operations on myapp models to 'other'""
		if model._meta.app_label == 'verifier':
			return 'voters'
		return None

	def allow_relation(self, obj1, obj2, **hints):
		""Allow any relation if a model in myapp is involved""
		if obj1._meta.app_label == 'verifier' or obj2._meta.app_label == 'verifier':
			return True
		return None

	def allow_syncdb(self, db, model):
		""Make sure the myapp app only appears on the 'other' db""
		if db == 'voters':
			return model._meta.app_label == 'verifier'
		elif model._meta.app_label == 'verifier':
			return False
		return None
}}}

The 'db' should be 'voters' when the 'app_label' of the model is 'verifier'. Since the fixture loading logic has the database set to 'default' it fails to load the fixture. For testing purposes I rewrote the 'allow_syncdb' function to always return None and as expected Django attempted to write the fixtures to the 'default' databases, instead of the one specified by the router."	New feature	closed	Testing framework	1.2	Normal	fixed		brenthoover	Accepted	0	0	0	0	0	0
