15

I'm trying to deploy my Django application to Heroku. The migrations are in my local Git. When I try:

git push heroku master
heroku run python manage.py syncdb

It applies the migrations and also promts me to create superuser, which I successfully do. Now the application is up and running, however when I try to log into the Django admin it's throwing:

OperationalError no such table: user_user

When I try

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

It applies all migrations, but fails to create superuser throwing:

django.db.utils.OperationalError: no such table: user_user

Either way I can not have my database set up and migrated on Heroku.

My database settings are:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

My user model is:

class User(AbstractUser):
    rating = models.PositiveIntegerField(default=settings.DEFAULT_USER_RATING)

Django version is 1.7.1.

How do I get my database tables created on Heroku?

Vadim Tikanov
  • 631
  • 1
  • 9
  • 26
  • I don't understand your question. You say that running `migrate` applies the migrations. Well, do that instead of syncdb, which is deprecated anyway. – Daniel Roseman Apr 21 '15 at 08:31
  • I have updated the question to become clearer. The issue is neither syncdb nor migrate help setting up the db on heroku. I still face "no such table: user_user" on trying to log into Django admin. – Vadim Tikanov Apr 21 '15 at 08:45
  • Possible duplicate of [Heroku created table but when I'll migrate, he says that doesn't created](http://stackoverflow.com/questions/15467389/heroku-created-table-but-when-ill-migrate-he-says-that-doesnt-created) – Ciro Santilli OurBigBook.com May 13 '16 at 13:53

4 Answers4

18

You must not use sqlite3 on Heroku.

sqlite stores the database as a file on disk. But the filesystem in a Heroku dyno is not persistent, and is not shared between dynos. So, when you do heroku run python manage.py migrate, Heroku spins up a new dyno with a blank database, runs the migrations, then deletes the dyno and the database. The dyno that's running your site is unaffected, and never gets migrated.

You must use one of the Heroku database add-ons. There is a free tier for Postgres. You should use the dj-database-url library to set your database settings dynamically from the environment variables which Heroku sets.

Also, for the same reason, you must do manage.py makemigrations locally, commit the result to git, then push to Heroku.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • I see now. Thanks Daniel. – Vadim Tikanov Apr 21 '15 at 09:15
  • I had exactly the same issue with Lumen/Laravel. Migrated and seeded locally, pushed to GitHub and then everything was working perfectly. – AnonymousAngelo Apr 08 '19 at 19:09
  • I had the same issue. I have added settings for postgreSql and added addon to heroku dyno. However, it still says no such table... – Matej J Aug 10 '20 at 13:17
  • This is correct and as said sqlite should NOT be used on Heroku (or anywhere production). However, IF you're just playing with a test Django app AND IF your local data doesn't have anything sensitive you could commit/push your local sqlite DB file and this data will be shown. However, as Daniel said this is ephemeral and not shared across Dyno. It's a BAD IDEA but it may be fun if you're learning Django. – Aldo 'xoen' Giambelluca Sep 14 '20 at 08:53
15

You can use postgresql:

In settings.py add(at the end of file):

# ie if Heroku server
if 'DATABASE_URL' in os.environ:
    import dj_database_url
    DATABASES = {'default': dj_database_url.config()}

In requirements.txt add:

dj-database-url 
psycopg2

Now you can run: heroku run python manage.py migrate

suhailvs
  • 20,182
  • 14
  • 100
  • 98
4

pip install django-heroku

Add import django_heroku at top of file settings.py

Place django_heroku.settings(locals()) at the very bottom of settings.py

It will automatically configure your db. You can learn more on their website

Damir Temir
  • 20
  • 1
  • 1
  • 5
Ayan Kurmanbay
  • 119
  • 2
  • 2
  • read a ton of other solutions but this was the only one that solved it. Looks like DATABASE_URL needs to be configured somehow to work with Heroku. Check the original documentation here: https://pypi.org/project/django-heroku/ dj_database_url seems to work as well: https://pypi.org/project/dj-database-url/ – Casey L Jul 01 '21 at 22:05
1

What version of django you are using..?

If you are using django>=1.7 you need to run migrate

After adding models you need to do python manage.py makemigrations then python manage.py migrate

If your project already contain migrations you can directly run python manage.py migrate command.

If you miss any step mentioned above please do that.

manikantanr
  • 574
  • 3
  • 13
  • Django version is 1.7.1. I have updated the question text - migrate applies the migrations, however createsuperuser fails after that. – Vadim Tikanov Apr 21 '15 at 08:47