Django on Heroku: relation does not exist

10,573

Solution 1

Make sure your local migration folder and content is under git version control.

If not, add, commit & push them as follows (assuming you have a migrations folder under <myapp>, and your git remote is called 'heroku'):

git add <myapp>/migrations/*
git commit -m "Fix Heroku deployment"
git push heroku

Wait until the push is successful and you get the local prompt back.

Then log in to heroku and there execute migrate. To do this in one execution environment, do not launch these as individual heroku commands, but launch a bash shell and execute both commands in there: (do not type the '~$', this represents the Heroku prompt)

heroku run bash
~$  ./manage.py migrate
~$  exit

Solution 2

You must not run makemigrations via heroku run. You must run it locally, and commit the result to git. Then you can deploy that code and run those generated migrations via heroku run python manage.py migrate.

The reason is that heroku run spins up a new dyno each time, with a new filesystem, so any migrations generated in the first command are lost by the time the second command runs. But in any case, migrations are part of your code, and must be in version control.

Solution 3

python manage.py makemigrations
python manage.py migrate
python manage.py migrate --run-syncdb

this worked for me.

Solution 4

As Heroku's dynos don't have a filesystem that persists across deploys, a file-based database like SQLite3 isn't going to be suitable. It's a great DB for development/quick prototypes, though. https://stackoverflow.com/a/31395988/784648

So between deploys your entire SQLite database is going to be wiped, you should move onto a dedicated database when you deploy to heroku I think. I know heroku has a free tier for postgres databases which I'd recommend if you just want to test deployment to heroku.

Share:
10,573
Manas Chaturvedi
Author by

Manas Chaturvedi

Software Engineer 2 at Haptik

Updated on June 04, 2022

Comments

  • Manas Chaturvedi
    Manas Chaturvedi almost 2 years

    I built a Django 1.9 project locally with sqlite3 as my default database. I have an application named Download which defines the DownloadedSongs table in models.py:

    models.py

    from __future__ import unicode_literals
    from django.db import models
    
    
    class DownloadedSongs(models.Model):
        song_name = models.CharField(max_length = 255)
        song_artist = models.CharField(max_length = 255)
    
        def __str__(self):
            return self.song_name + ' - ' + self.song_artist
    

    Now, in order to deploy my local project to Heroku, I added the following lines at the bottom of my settings.py file:

    import dj_database_url
    DATABASES['default'] =  dj_database_url.config()
    

    My application has a form with a couple of text fields, and on submitting that form, the data gets inserted into the DownloadedSongs table. Now, when I deployed my project on Heroku and tried submitting this form, I got the following error:

    Exception Type: ProgrammingError at /download/
    Exception Value: relation "Download_downloadedsongs" does not exist
    LINE 1: INSERT INTO "Download_downloadedsongs" ("song_name", "song_a...
    

    This is how my requirements.txt file looks like:

    beautifulsoup4==4.4.1
    cssselect==0.9.1
    dj-database-url==0.4.1
    dj-static==0.0.6
    Django==1.9
    django-toolbelt==0.0.1
    gunicorn==19.6.0
    lxml==3.6.0
    psycopg2==2.6.1
    requests==2.10.0
    static3==0.7.0
    

    Also, I did try to run the following commands as well:

    heroku run python manage.py makemigrations
    heroku run python manage.py migrate
    

    However, the issue still persists. What seems to be wrong here?

  • aleksk
    aleksk over 6 years
    For an issue I had in testing, I needed to login to the shell to do the migrations. I did the steps above, but instead of using "./manage.py" i used "python manage.py" (using ./ gave me a permission error). Thanks.