﻿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
15568	IntegrityError rises from test test_generic_key_deletion when it runs with the test test_generic_key_cross_database_protection for the INNODB	rahul.priyadarshi@…	nobody	"When i ran test cases of multiple_databases model of regressiontests then ""test_generic_key_deletion"" test case gave Error at the point where it was trying to create Review object using ""other"" db, but when i ran ""test_generic_key_deletion"" test case only then it passed perfectly.

When i narrowed the problem and run this test case with ""test_generic_key_cross_database_protection"" then even it gives Error at same line.

In my investigation i found that in ""test_generic_key_cross_database_protection"" test case, one Reiview object is getting created and because there exist FK relationship with ContentType, content type record for this is getting populated in DB and content type lookup are cached. At the end of this test case, content type record gets cleared by rollback, but content type lookup still remain in cache, so that when inside ""test_generic_key_deletion"" it tries to create Review object it gets content type from the cache and does not have  to go to  DB. Next when it tries to populate review table, Integrity error is raised as it has not made entry to contenttype table.

{{{
$ python runtests.py --settings=settings_mysql multiple_database.QueryTestCase.test_generic_key_cross_database_protection multiple_database.QueryTestCase.test_generic_key_deletion


Creating test database for alias 'default'...
Creating test database for alias 'other'...
.E
======================================================================
ERROR: test_generic_key_deletion (regressiontests.multiple_database.tests.QueryTestCase)
Cascaded deletions of Generic Key relations issue queries on the right database
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""/home/.../.../.../Django-1.3-rc-1/tests/regressiontests/multiple_database/tests.py"", line 825, in test_generic_key_deletion
    review = Review.objects.using('other').create(source=""Python Weekly"", content_object=dive)
  File ""/home/.../.../.../lib/python2.5/site-packages/django/db/models/query.py"", line 360, in create
    obj.save(force_insert=True, using=self.db)
  File ""/home/.../.../.../lib/python2.5/site-packages/django/db/models/base.py"", line 460, in save
    self.save_base(using=using, force_insert=force_insert, force_update=force_update)
  File ""/home/.../.../.../lib/python2.5/site-packages/django/db/models/base.py"", line 553, in save_base
    result = manager._insert(values, return_id=update_pk, using=using)
  File ""/home/.../.../.../lib/python2.5/site-packages/django/db/models/manager.py"", line 195, in _insert
    return insert_query(self.model, values, **kwargs)
  File ""/home/.../.../.../lib/python2.5/site-packages/django/db/models/query.py"", line 1436, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File ""/home/.../.../.../lib/python2.5/site-packages/django/db/models/sql/compiler.py"", line 791, in execute_sql
    cursor = super(SQLInsertCompiler, self).execute_sql(None)
  File ""/home/.../.../.../lib/python2.5/site-packages/django/db/models/sql/compiler.py"", line 735, in execute_sql
    cursor.execute(sql, params)
  File ""/home/.../.../.../lib/python2.5/site-packages/django/db/backends/mysql/base.py"", line 86, in execute
    return self.cursor.execute(query, args)
  File ""/home/.../.../.../lib/python2.5/site-packages/MySQL_python-1.2.3-py2.5-linux-x86_64.egg/MySQLdb/cursors.py"", line 174, in execute
    self.errorhandler(self, exc, value)
  File ""/home/.../.../.../lib/python2.5/site-packages/MySQL_python-1.2.3-py2.5-linux-x86_64.egg/MySQLdb/connections.py"", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails (`test_testpy2/multiple_database_review`, CONSTRAINT `content_type_id_refs_id_82d6f7ab` FOREIGN KEY (`content_type_id`) REFERENCES `django_content_type` (`id`))')

----------------------------------------------------------------------
Ran 2 tests in 0.040s

FAILED (errors=1)
Destroying test database for alias 'default'...
Destroying test database for alias 'other'...
}}}

I am setting storage engine through OPTIONS key of the DATABASES setting as follows:

{{{
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'djangodb1',
        'USER': '****',
        'PASSWORD': '****',
        'HOST': '****',
        'PORT': '****',
        'OPTIONS': {
                'init_command': 'SET storage_engine=INNODB',
        },
    },
    'other': {
	'ENGINE': 'django.db.backends.mysql',
        'NAME': 'djangodb1', 
        'USER': '****', 
        'PASSWORD': '****',
        'HOST': '****',
        'PORT': '****',
        'OPTIONS': {
                'init_command': 'SET storage_engine=INNODB',
        },
	}
}
}}}

"	Bug	closed	contrib.contenttypes	1.3-rc	Normal	fixed			Accepted	0	0	0	0	0	0
