django.auth create_permissions must clear the content type cache before creating permissions
|Reported by:||Sean Legassick||Owned by:|
|Cc:||jodym@…, chris+django@…, rjalves@…, jorgecarleitao, julenx@…, Christoph Heer, Jonas Trappenberg||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 8 years ago by
comment:23 Changed 15 months ago by
|Owner:||changed from nobody to Koen Vossen|
|Status:||new → assigned|
comment:24 Changed 15 months ago by
|Owner:||Koen Vossen deleted|
|Status:||assigned → new|