Django: relation "django_site" does not exist
Question:
I am running a test django server on aws and I just installed django-userena and when I try to signup a user upon clicking submit, I get the following message:
relation “django_site” does not exist LINE 1:
…”django_site”.”domain”, “django_site”.”name” FROM “django_si…
I am not really sure what went wrong here. I did some researching and added " 'django.contrib.sites',"
to my installed apps, but I am still getting the error. I will there is an extra step I am missing. Any suggestions or advice?
Answers:
You may be calling a site object before creating site model(before syncdb or migrate)
ex: site = Site.objects.get(id=settings.SITE_ID)
I have the same problem and fixed it like this:
- add
SITE_ID=1
into settings.py
-
run this command :
python manage.py migrate
This issue might be caused by one of the apps you’re using. If you check the traceback carefully, you might already find the delinquent.
I had those issues using django-debug-toolbar
and zinnia
.
If you are using the django-debug-toolbar
this might be a solution:
Try following the steps for the explicit setup: http://django-debug-toolbar.readthedocs.org/en/1.2.2/installation.html#explicit-setup
Alternatively remove debug_toolbar
from your INSTALLED APPS
.
If that doesn’t help or if another app is causing the issue, try to temporarily remove all imports (e.g. installed app, urls, custom views, settings), which are displayed in the traceback.
I recently ran into this issue (Django 1.8.7) even with SITE_ID = 1
in my settings. I had to manually migrate the sites
app before any other migrations:
./manage.py migrate sites
./manage.py migrate
A horrible code lead to this error for me. I had a global variable to get the current site
SITE = Site.objects.get(pk=1)
this was evaluated during migration and lead to the error.
I’m late, but I ran into the same issue with django v 1.11.
The issue was that I was rebuilding a model outside the normal def() and in a form() [I use the models for a choice] The traceback should have the .py file listed
e.g.
File "filepath/views.py", line 67, in <module>
some_variable = some_model.objects.get(name ='name')
So I had to comment it out to rebuild my migrations
Going to leave this here for future me:
python manage.py makemigrations allauth
This worked for me, I forgot why, took me too long to figure out how I fixed this the first time
Edit: makemigrations sometimes doesnt make 3rd party things like allauth which some of my projects use, so I have to specify those ones
I experienced the same problem with creating a new empty database for my project (which uses zinnia)
Running ‘manage migrate site’ before ‘manage migrate’ did not solve anything. It seems that the complete project was loaded before any table creating was done.
I resolved to catching the errors that importing the zinnia releated app produced.
e.g.:
in the urls.py of the app
urlpatterns = None
app_name = 'something'
try:
from .views import MyEntryCreate
urlpatterns = [
url(r'^blogentry/create/$',
login_required(MyEntryCreate.as_view()),
name='zinnia_entry-add'),
]
except Exception as e:
logger.error(app_name+" Error urls: "+str(e))
urlpatterns = []
Had to do something like that elsewhere in that app, and ‘manage migrate’ worked again.
if you are getting this error when deploying you django app to Heroku, make sure you have run:
heroku run python manage.py migrate
This worked for me
I got this error while working with django-cookiecutter
, django-allauth
and django-rest-auth
I literally spent 5 hours pulling my hair out. Eventually gave in and started to comment out bit by bit
What worked for me was commenting out both pre-configured url paths (they come with cookiecutter Django):
# User management
path("users/", include("yourapp.users.urls")),
path("accounts/", include("allauth.urls")),
After that migrations worked.
I uncommented it and my app has worked ever since. It was only for the initial migration
Hope it helps someone!
this is basically database error i can able to fix this for this time, database dosent exist? why i dont know if you know please
find and remove it Site.objects.get_current()
comment it if you know, and after you run ./manage.py migrate
[step:2]i will say it already exists. so i did ./manage.py migrate --fake
[step:3]
[step:1]
su postgres
psql -U <username> -d <database>
SELECT * FROM django_site;
CREATE TABLE django_site(
id serial PRIMARY KEY,domain VARCHAR (50) NULL,
name VARCHAR (50) NULL);
INSERT INTO django_site VALUES('1','mysite.com', 'mysite');
q
after you change your mysite.com value from admin enterface .
I just restarted my computer and problem disappeared 🙂
(restarting docker-compose is not enough).
Posting this in case that someone lands on this problem and none of the answers above have worked.
This is an addition to one of the solutions that may or may not solve your problem:
The solution being: make sure that you do the migrate command first before anything else!
So in the usual case, you upload your code to the cloud, which could be AWS. You do the usual docker-compose commands right, well you should do exactly as the following first:
Do the following steps:
docker-compose -f production.yml build
then docker-compose -f production.yml run --rm django python manage.py migrate
And then after that you can now run the docker instance via docker-compose -f production.yml up
or docker-compose -f production.yml up -d
(search on google what that means)
—
If you’ve already run the instance before migrating, then just stop the docker instance that you’re running and remove it. A handy tutorial for docker commands I’ve found is this: https://www.thegeekdiary.com/how-to-list-start-stop-delete-docker-containers/
I am running a test django server on aws and I just installed django-userena and when I try to signup a user upon clicking submit, I get the following message:
relation “django_site” does not exist LINE 1:
…”django_site”.”domain”, “django_site”.”name” FROM “django_si…
I am not really sure what went wrong here. I did some researching and added " 'django.contrib.sites',"
to my installed apps, but I am still getting the error. I will there is an extra step I am missing. Any suggestions or advice?
You may be calling a site object before creating site model(before syncdb or migrate)
ex: site = Site.objects.get(id=settings.SITE_ID)
I have the same problem and fixed it like this:
- add
SITE_ID=1
intosettings.py
-
run this command :
python manage.py migrate
This issue might be caused by one of the apps you’re using. If you check the traceback carefully, you might already find the delinquent.
I had those issues using django-debug-toolbar
and zinnia
.
If you are using the django-debug-toolbar
this might be a solution:
Try following the steps for the explicit setup: http://django-debug-toolbar.readthedocs.org/en/1.2.2/installation.html#explicit-setup
Alternatively remove debug_toolbar
from your INSTALLED APPS
.
If that doesn’t help or if another app is causing the issue, try to temporarily remove all imports (e.g. installed app, urls, custom views, settings), which are displayed in the traceback.
I recently ran into this issue (Django 1.8.7) even with SITE_ID = 1
in my settings. I had to manually migrate the sites
app before any other migrations:
./manage.py migrate sites
./manage.py migrate
A horrible code lead to this error for me. I had a global variable to get the current site
SITE = Site.objects.get(pk=1)
this was evaluated during migration and lead to the error.
I’m late, but I ran into the same issue with django v 1.11.
The issue was that I was rebuilding a model outside the normal def() and in a form() [I use the models for a choice] The traceback should have the .py file listed
e.g.
File "filepath/views.py", line 67, in <module>
some_variable = some_model.objects.get(name ='name')
So I had to comment it out to rebuild my migrations
Going to leave this here for future me:
python manage.py makemigrations allauth
This worked for me, I forgot why, took me too long to figure out how I fixed this the first time
Edit: makemigrations sometimes doesnt make 3rd party things like allauth which some of my projects use, so I have to specify those ones
I experienced the same problem with creating a new empty database for my project (which uses zinnia)
Running ‘manage migrate site’ before ‘manage migrate’ did not solve anything. It seems that the complete project was loaded before any table creating was done.
I resolved to catching the errors that importing the zinnia releated app produced.
e.g.:
in the urls.py of the app
urlpatterns = None
app_name = 'something'
try:
from .views import MyEntryCreate
urlpatterns = [
url(r'^blogentry/create/$',
login_required(MyEntryCreate.as_view()),
name='zinnia_entry-add'),
]
except Exception as e:
logger.error(app_name+" Error urls: "+str(e))
urlpatterns = []
Had to do something like that elsewhere in that app, and ‘manage migrate’ worked again.
if you are getting this error when deploying you django app to Heroku, make sure you have run:
heroku run python manage.py migrate
This worked for me
I got this error while working with django-cookiecutter
, django-allauth
and django-rest-auth
I literally spent 5 hours pulling my hair out. Eventually gave in and started to comment out bit by bit
What worked for me was commenting out both pre-configured url paths (they come with cookiecutter Django):
# User management
path("users/", include("yourapp.users.urls")),
path("accounts/", include("allauth.urls")),
After that migrations worked.
I uncommented it and my app has worked ever since. It was only for the initial migration
Hope it helps someone!
this is basically database error i can able to fix this for this time, database dosent exist? why i dont know if you know please
find and remove it Site.objects.get_current()
comment it if you know, and after you run ./manage.py migrate
[step:2]i will say it already exists. so i did ./manage.py migrate --fake
[step:3]
[step:1]
su postgres
psql -U <username> -d <database>
SELECT * FROM django_site;
CREATE TABLE django_site(
id serial PRIMARY KEY,domain VARCHAR (50) NULL,
name VARCHAR (50) NULL);
INSERT INTO django_site VALUES('1','mysite.com', 'mysite');
q
after you change your mysite.com value from admin enterface .
I just restarted my computer and problem disappeared 🙂
(restarting docker-compose is not enough).
Posting this in case that someone lands on this problem and none of the answers above have worked.
This is an addition to one of the solutions that may or may not solve your problem:
The solution being: make sure that you do the migrate command first before anything else!
So in the usual case, you upload your code to the cloud, which could be AWS. You do the usual docker-compose commands right, well you should do exactly as the following first:
Do the following steps:
docker-compose -f production.yml build
then docker-compose -f production.yml run --rm django python manage.py migrate
And then after that you can now run the docker instance via docker-compose -f production.yml up
or docker-compose -f production.yml up -d
(search on google what that means)
—
If you’ve already run the instance before migrating, then just stop the docker instance that you’re running and remove it. A handy tutorial for docker commands I’ve found is this: https://www.thegeekdiary.com/how-to-list-start-stop-delete-docker-containers/