﻿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
6259	ContentType.objects.get_for_model will cause the error in django.test.TestCase	bear330	nobody	"If you call ContentType.objects.get_for_model in test case which extends django.test.TestCase, it will:

_mysql_exceptions.IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails (`mercurius/auth_permission`, CONSTRAINT `content_type_id_refs_id_728de91f` FOREIGN KEY (`content_type_id`) REFERENCES `django_content_type` (`id`))')

This is because the ContentType.objects.get_for_model will cache the result. The django.test.TestCase truncates all table after run test method and call django.contrib.auth.management.create_permissions which will call ContentType.objects.get_for_model.

At this time, ContentType.objects.get_for_model won't create new content type data into django_contenttype table, it will get the ContentType object from cache. 

Permission.objects.get_or_create will occur database constraint error.

All the code which call ContentType.objects.get_for_model can not be called in django.test.TestCase, this is a big trouble.
"	Uncategorized	closed	Testing framework	dev	Normal	worksforme	ContentType, permissions. constraint		Unreviewed	0	0	0	0	0	0
