2

I've set up a new Django project (based on the template django cookie cutter) but get a postgres error when trying to migrate the database for the first time

psycopg2.ProgrammingError: relation "django_site_domain_a2e37b91_uniq" already exists

I'm not clear what the issue is or how best to diagnose?

das-g
  • 9,718
  • 4
  • 38
  • 80
Yunti
  • 6,761
  • 12
  • 61
  • 106

1 Answers1

0

What is SITE_ID?

Django was created from a set of scripts developed at a newspaper to publish content on multiple domains; using one single content base.

This is where the "sites" module comes in. Its purpose is to mark content to be displayed for different domains.

In previous versions of django, the startproject script automatically added the django.contrib.sites application to INSTALLED_APPS, and when you did syncdb, a default site with the URL example.com was added to your database, and since this was the first site, its ID was 1 and that's where the setting comes from.

Keep in mind that starting from 1.6, this framework is not enabled by default. So if you need it, you must enable it

The SITE_ID setting sets the default site for your project. So, if you don't specify a site, this is the one it will use.

Ways to fix this:

Increment SITE_ID:

1. Increment `SITE_ID` variable in settings.py
2. python manage.py makemigrations
3. python manage.py migrate --run-syncdb

Cons: Had to increment SITE_ID without good reason

Attempt to migrate without --run-syncdb:

1. python manage.py makemigrations
2. python manage.py migrate

Note: May have to try multiple times before it works. Unsure why, possibly because I was in the process of deleting the pvc

arshbot
  • 12,535
  • 14
  • 48
  • 71
  • Additional note: It could be that the second step worked because I performed the first step beforehand - meaning the error occurs when the migration from SITE_ID 1 -> 1 but going 1 -> 2 then 2 -> 1 avoids the issue – arshbot Sep 12 '19 at 15:17