Code

Opened 7 years ago

Closed 6 years ago

Last modified 17 months 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: master
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.

Attachments (0)

Change History (2)

comment:1 Changed 6 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to worksforme
  • Status changed from new to closed

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

comment:2 Changed 17 months ago by rjalves

  • Easy pickings unset
  • Severity set to Normal
  • Type set to Uncategorized
  • UI/UX unset

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.