django.auth create_permissions must clear the content type cache before creating permissions
|Reported by:||seanl||Owned by:|
|Cc:||jodym@…, chris+django@…, rjalves@…, jorgecarleitao, julenx@…, jarus, teeberg||Triage Stage:||Accepted|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||yes||Patch needs improvement:||no|
I hit a problem which took some time to track down, where at the DB flush stage in a sequence of tests (using TransactionTestCase) the recreation of permissions was failing with a FK constraint error.
This was caused by inserting a permission referring to a content type that didn't exist in the DB.
This happened because the content type was still in the cache, even thought the django_content_type table had been truncated.
The cache hadn't been cleared because post_syncdb signal dispatch had called create_permissions before calling update_contenttypes (which does clear the cache and recreate the content types correctly).
The real problem here is that create_permissions and update_contenttypes are both connected to the post_syncdb signal, with the former depending on the latter having been run first, but the dispatcher doesn't guarantee order of dispatch (or rather it dispatches in the order the signal handlers are connected, but that order depends on the order in which modules are loading which is not well-defined). Unfortunately that's a hard problem to solve, and I don't have any ideas about that short of substantive changes to the signal dispatcher.
An easier solution to this particular problem is for create_permissions to clear the content types cache before it recreates permissions, that way the necesarry content types will be created as needed (see attached patch).
Change History (28)
Changed 7 years ago by seanl
comment:1 Changed 7 years ago by seanl
- Has patch set
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
Changed 4 years ago by rjalves
comment:12 Changed 4 years ago by rjalves
- Cc rjalves@… added
comment:23 Changed 10 months ago by kvsn
- Owner changed from nobody to kvsn
- Status changed from new to assigned
comment:24 Changed 10 months ago by kvsn
- Owner kvsn deleted
- Status changed from assigned to new