jeffery_the_wind
jeffery_the_wind

Reputation: 18228

django makemigrations and migrate on heroku server don't create tables

Python Version 2.7 Django Version 1.9.7

I have created a Django app on heroku. I can't get the heroku server to migrate properly. In the past I have done all the makemigrations locally and then pushed them to the server. It has worked in the past. Now I thought I would choose to do the migrations all on the server side, since I am not running this app locally at all.

I just created one new model inside the models.py for app 'main':

from __future__ import unicode_literals

from django.db import models

class InstagramPhotos(models.Model):
    imageId = models.IntegerField()
    userId = models.IntegerField()
    likes = models.IntegerField()
    captionText = models.CharField(max_length=200)
    image = models.ImageField()

After pushing changes to the server, I ran this, with following output:

heroku run python manage.py makemigrations main

Running python manage.py makemigrations main on ⬢ glacial-beach-50253... up, run.8354 Migrations for 'main':
0001_initial.py: - Create model InstagramPhotos

Seems ok right? So then I try to migrate which as you know will actually create the tables in the DB:

heroku run python manage.py migrate

Running python manage.py migrate on ⬢ glacial-beach-50253... up, run.7556 Operations to perform: Apply all migrations: auth, contenttypes, admin, sessions Running migrations: No migrations to apply. Your models have changes that are not yet reflected in a migration, and so won't be applied. Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.

No matter how many times I have tried to re-run the makemigrations and then migrate it still seems to not pick it up. Not sure why this is happening besides it is just not possible to operate on heroku server this way? Do I definitely need to do makemigrations locally and push?

FYI I just have the default sqlite3 DB still defined in settings.py.

Upvotes: 16

Views: 25251

Answers (5)

anaco ndeda
anaco ndeda

Reputation: 29

  1. Run these commands locally:
python manage.py makemigrations 
python manage.py migrate
  1. Commit your code

  2. Push it to Heroku master

  3. Run

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

Your issue should be resolved now.

Upvotes: 1

hmfzer
hmfzer

Reputation: 11

Run makemigrations and migrate, then pushed to remote repo.

Upvotes: 1

Kennedy Lucas
Kennedy Lucas

Reputation: 21

I struggled recently with a similar error and ended discovering that i mistakenly have been git ignoring init.py files. Without these files in the migrations folder, the migrate command cannot find the migration files and execute them.

Upvotes: 1

ac_baz
ac_baz

Reputation: 39

I ran into the same error two times while deploying to Heroku. While listing the files under the app directory, I found out db.sqlite3 file was missing which is necessary for django to create the sql tables. While it automatically creates the file in local, it wasn't getting created on the heroku while running migrate command.

There are two options from here:

  1. create db.sqlite3 file on heroku under your app directory
  2. run "python manage.py migrate" locally to create the db.sqlite3 and push it to the heroku repo

Upvotes: 0

Dishant Chavda
Dishant Chavda

Reputation: 456

I had a similar problem.

I just ssh into heroku dyno with: heroku run bash ( from your heroku application folder ofc )

Than run all the migration and makemigration commands with createsuperuser if needed. Works with sqlite and postgre for me.

Upvotes: 28

Related Questions