[Solved]-Django DatabaseError: relation "django_site"

2👍

not been able to solve this a django way so i tried using sql, i created a dump of just the database like this.

pg_dump mypgdatabase | gzip -c > mypgdatabase.dump.out.gz

then moved it to the server

scp /path/to/mypgdatabase.dump.out.gz  my_remote_server

then recreated it on the server like this

psql -d mypgdatabase -f mypgdatabase.dump.out

then run

./manange.py migrate --all

and all when well.

8👍

You may be calling a site object before creating site model(before syncdb)

ex: site = Site.objects.get(id=settings.SITE_ID)

5👍

This issue continues to plague many, including myself. Although a tedious process, this approach saves me the brain power:

Disable all external apps in your INSTALLED_APPS, except your own apps, like so:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'django.contrib.flatpages',
    'main', # This is my own app.

    # 'compressor',
    # 'ckeditor',
    # 'imagekit',
    # 'debug_toolbar',
    # 'rest_framework',
    # 'allauth',
    # 'allauth.account',
    # 'allauth.socialaccount',

    # 'allauth.socialaccount.providers.google',
    # 'allauth.socialaccount.providers.facebook',
)

Run

python manage.py makemigrations
python manage.py migrate

Then, uncomment all the other apps, then repeat makemigrations and migrate above.

That works all the time for me

👤KhoPhi

4👍

I can’t see your models or what apps are you using, but my guess is that you are using django_site (Site model) and you don’t have ‘django.contrib.sites’ in the INSTALLED_APPS.

If I’m correct then just add 'django.contrib.sites', to your INSTALLED_APPS.

4👍

Had this strange issue while initiating a new database and using django-debug-toolbar. Removed it from the INSTALLED_APPS and was able to run syncdb. Then re-added debug_toolbar and it was still working fine.

If you’re using django-debug-toolbar, try to comment out debug_toolbar in your installed apps and try again.


Update: Please follow the steps for the explicit setup: http://django-debug-toolbar.readthedocs.org/en/1.2.2/installation.html#explicit-setup

3👍

i have same problem and fixed it like this:

  1. add SITE_ID=1 into settings.py
  2. run this command :

    python manage.py migrate

1👍

Couple of things you can try and check:

  • Your settings.py should have a SITE_ID usually = 1
  • Change order of your INSTALLED_APPS in your settings.py and try temporarily commenting items out.
  • As Geo said, check that you’re not calling a site object before creating site model (ex: site = Site.objects.get(id=settings.SITE_ID)).

Once you got it working, remember to manage.py makemigrations APP_NAME as I found cases where it seem to have then avoided the commenting out step.

0👍

I overcame this issue with the following order in INSTALLED_APPS:

INSTALLED_APPS = (
    'django.contrib.sites',
    'allauth',                                                                 
    'allauth.account',                                                         
    # my other apps,
)

0👍

My "commenting out" pattern was slightly different than the accepted answers (as shown below). That is to say, it might be unique depending on the values of various dependencies scattered throughout your app. If commenting out the values above throws an error message, I recommend commenting out the apps that throw the error message and uncommenting accordingly until it works for you. This may be a little "brute force" but it should get the job done.

INSTALLED_APPS = [
    # django native apps
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    "django.forms",
    'django.contrib.gis',
    # 'django.contrib.flatpages',
    #third party apps
    #django-allauth apps
    'allauth',
    # 'allauth.account',
    # 'allauth.socialaccount',
    # 'allauth.socialaccount.providers.facebook',
    # 'allauth.socialaccount.providers.google',
    # 'allauth.socialaccount.providers.twitter',
    # 'allauth.socialaccount.providers.github',
    # my apps
    'app0',
    'app1',]

#MIGRATION_MODULES = {"sites": "mysite.contrib.sites.migrations"}

-2👍

Also make sure SITE_ID = 1 comes before the DATABASE definition in settings.py

👤nimzo

Leave a comment