Opened 3 years ago

Last modified 3 years ago

#21278 new New feature

Using dumpdata to create unit test fixtures causes duplicate foreign keys for auth permissions. Excluding auth causes other referenced auth models to be missing.

Reported by: ellisd23@… Owned by: nobody
Component: contrib.auth Version: 1.5
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


This seems to be a pretty fundamental issue, since models will frequently reference User. There are many instances of users having frustrations with contrib apps that are auto-generated upon migration/sync (e.g. contenttypes).

For contenttypes, this issue can be mitigated by excluding the app and using --natural. But for contrib.auth, if the app is excluded, any referenced User objects in fixture data will be missing. If the app is not excluded, duplicate permissions will be created, since the post-migration signal will have already created them. Since there is a unique key restraint, the importing of the fixture data will fail. It's a catch 22.

Developers can manually remove the permissions from the fixture data, or override fixture loading in their tests to remove the data, but it seems to me that since this is such a basic piece of functionality, there should be a simple interface to do such a thing. It'd be nice to have something simpler for contenttypes as well, rather than "--exclude contenttypes --natural".

Change History (4)

comment:1 Changed 3 years ago by akaariai

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

I agree that fixture loading + contenttypes / permissions is a problem. I am marking this as accepted, although I don't have any idea what could be done here.

comment:2 Changed 3 years ago by carljm

  • Type changed from Uncategorized to New feature

comment:3 Changed 3 years ago by russellm

I'm confused by this report. This is the exact problem that led to the introduction of natural foreign keys in the first place, and I was under the impression that it was fixed. Permissions have natural keys defined, so they should be able to be dumped with --natural.

Has something regressed in the 3-4 years since we committed natural keys, or have I missed an important detail about this report?

comment:4 Changed 3 years ago by ellisd23@…

It's been a while since I made the ticket, but IIRC the issue is that even with --natural passed, the problem is that an attempt is made to create permissions twice: once during the loading of the fixtures, and once automatically. It's that second time that causes duplicate key errors.

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