Skip to content
Advertisement

How to populate auth.groups and auth.group_permissions tables with initial data in Django project

Context

I am using PostgreSQL.

I would like to immediately populate the auth.groups and auth.group_permissions tables, after the very first migration of my project.

So, after I have set up these tables the way I want them, I dump them by running

python manage.py dumpdata auth.groups auth.group_permissions > core/fixtures/initial_data.json

The resulting file is this

[
  {
    "model": "auth.group",
    "pk": 1,
    "fields": {
      "name": "Admin",
      "permissions": [
        29,
        31,
        32,
        33,
        34,
        35,
        36
      ]
    }
  },
  {
    "model": "auth.group",
    "pk": 2,
    "fields": {
      "name": "Operators",
      "permissions": [
        42,
        43,
        44,
        39,
        40
      ]
    }
  },
  {
    "model": "auth.group_permissions",
    "pk": 12,
    "fields": {
      "group": 1,
      "permission": 32
    }
  },
  {
    "model": "auth.group_permissions",
    "pk": 13,
    "fields": {
      "group": 1,
      "permission": 33
    }
  },
  {
    "model": "auth.group_permissions",
    "pk": 14,
    "fields": {
      "group": 1,
      "permission": 34
    }
  },
  {
    "model": "auth.group_permissions",
    "pk": 15,
    "fields": {
      "group": 1,
      "permission": 35
    }
  },
  {
    "model": "auth.group_permissions",
    "pk": 16,
    "fields": {
      "group": 1,
      "permission": 36
    }
  },
  {
    "model": "auth.group_permissions",
    "pk": 17,
    "fields": {
      "group": 1,
      "permission": 29
    }
  },
  {
    "model": "auth.group_permissions",
    "pk": 18,
    "fields": {
      "group": 1,
      "permission": 31
    }
  },
  {
    "model": "auth.group_permissions",
    "pk": 19,
    "fields": {
      "group": 2,
      "permission": 39
    }
  },
  {
    "model": "auth.group_permissions",
    "pk": 20,
    "fields": {
      "group": 2,
      "permission": 40
    }
  },
  {
    "model": "auth.group_permissions",
    "pk": 21,
    "fields": {
      "group": 2,
      "permission": 42
    }
  },
  {
    "model": "auth.group_permissions",
    "pk": 22,
    "fields": {
      "group": 2,
      "permission": 43
    }
  },
  {
    "model": "auth.group_permissions",
    "pk": 23,
    "fields": {
      "group": 2,
      "permission": 44
    }
  }
]

The problem

To try this, I drop and recreate my db, so that it’s completely fresh, and after the first migrate command, I run

python manage.py loaddata initial_data.json

And here I get the error

django.db.utils.IntegrityError: Problem installing fixture ‘/home/uname/Workspace/project/core/fixtures/initial_data.json’:

Could not load auth.Group_permissions(pk=12): duplicate key value violates unique constraint “auth_group_permissions_group_id_permission_id_0cd325b0_uniq” DETAIL: Key (group_id, permission_id)=(1, 32) already exists.

which is pretty confusing for me. How can it throw about a duplicate key when these 2 tables are completely empty at this point? How do I get it to populate these two tables with these initial data?

Thanks

Advertisement

Answer

The fixture is loaded in a single transaction and there is duplicate data in your dump.

The first auth.group entry with name “Admin” defines all of it’s permissions in a list, this includes the permission with id 32. The first auth.group_permissions entry then duplicates this data which cause the exception as the same data was loaded earlier in the transaction

Removing all of the auth.group_permissions entries from the dump JSON or excluding auth.group_permissions from the dumpdata command is the solution

Advertisement