On Wed, May 5, 2010 at 1:43 AM, J. Cliff Dyer <j...@sdf.lonestar.org> wrote:
> I'm having trouble working with multi-db using mysql replication.  When
> I run our test suite, I'm getting several hundred errors if I have more
> than one database configured, and none if I have only one configured.
>
> It seems that something isn't getting properly cleared out between test
> cases, so duplicate objects are getting created.
>
> My DATABASES setting looks like this:
>
>
> DATABASES = {
>    'default': {
>        'ENGINE': 'django.db.backends.mysql',
>        'HOST': 'jellyroll',
>        'NAME': 'ee',
>        'USER': 'ee',
>        'PASSWORD': 'ee',
>        'OPTIONS': {
>            'init_command': 'SET storage_engine=INNODB',
>            'charset' : 'utf8',
>            'use_unicode' : True,
>        },
>    },
>    'replicant0': {
>        'ENGINE': 'django.db.backends.mysql',
>        'HOST': 'jellyroll-rep0',
>        'NAME': 'test',
>        'USER': 'ee',
>        'PASSWORD': 'ee',
>        'TEST_MIRROR': 'default',
>        'OPTIONS': {
>            'init_command': 'SET storage_engine=INNODB',
>            'charset' : 'utf8',
>            'use_unicode' : True,
>        },
>    },
> }
>
>
> My router looks like this:
>
> class PrimaryReplicantRouter(object):
>    """Set up routing with one primary database, and zero or more
>    replicant databases."""
>
>    db_list = settings.DATABASES.keys()
>    primary_db = 'default'
>    replicant_db_list = [x for x in db_list if x != primary_db]
>
>    def db_for_read(self, model, **hints):
>        try:
>            return random.choice(self.replicant_db_list)
>        except IndexError:
>            return self.primary_db
>
>
>    def db_for_write(self, model, **hints):
>        """Write to the Primary DB"""
>        return self.primary_db
>
>    def allow_relation(self, obj1, obj2, **hints):
>        """Allow a relationship between any two objects in the db
>        pool"""
>
>        if (obj1._state.db in self.db_list
>            and obj2._state.db in self.db_list):
>            return True
>        return None
>
>    def allow_syncdb(self, db, models):
>        """All writes go to the primary db."""
>        return db == self.primary_db
>
> The first error that comes out of the test suite is a duplicate username
> in auth during a testcase's setUp, which makes it look like the object
> is getting created on one test pass, and then created again the next
> time around without deleting it properly:
>
>
> $ ./manage.py test --failfast
> .....E
> ======================================================================
> ERROR: Regressiontest for #12462
> ----------------------------------------------------------------------
> Traceback (most recent call last):
>  File
> "/home/cliff/projects/lr-mdb/lexilereader/web/../contrib/django/contrib/auth/tests/auth_backends.py",
>  line 14, in setUp
>    User.objects.create_user('test', 't...@example.com', 'test')
> ...
> IntegrityError: (1062, "Duplicate entry 'test' for key 2")
>
>
> Any ideas what's going wrong?

I can't say I've seen this problem myself. Some more details might
help narrow down the problem:

 * Is your test case marked as a multidb test? (i.e., multidb=True on
the test case itself)

 * Can you describe your database configuration in more detaill?
Specifically, when the test framework creates the test database
(presumably on 'jellyroll') does a slave also get created
automatically? Is the test slave automatically paired to the test
master?

Yours,
Russ Magee %-)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to