Opened 17 years ago

Closed 17 years ago

Last modified 12 years ago

#6259 closed Uncategorized (worksforme)

ContentType.objects.get_for_model will cause the error in django.test.TestCase

Reported by: bear330 Owned by: nobody
Component: Testing framework Version: dev
Severity: Normal Keywords: ContentType, permissions. constraint
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Change History (2)

comment:1 by Russell Keith-Magee, 17 years ago

Resolution: worksforme
Status: newclosed

I can't reproduce this problem. If you can provide an actual test case that fails, feel free to reopen this ticket.

comment:2 by Renato Alves, 12 years ago

Easy pickings: unset
Severity: Normal
Type: Uncategorized
UI/UX: unset

Same problem is reported on #10827 with better test case.

Note: See TracTickets for help on using tickets.
Back to Top