django.auth create_permissions must clear the content type cache before creating permissions
|Reported by:||seanl||Owned by:||nobody|
|Cc:||jodym@…, chris+django@…, rjalves@…, jorgecarleitao, julenx@…, jarus||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 (19)
Changed 6 years ago by seanl
comment:1 Changed 6 years ago by seanl
- Has patch set
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
Changed 2 years ago by rjalves
comment:12 Changed 2 years ago by rjalves
- Cc rjalves@… added