'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
.
Solution 1:[1]
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.
Solution 2:[2]
- Run these commands locally
python manage.py makemigrations,,, Python manage.py migrate
2.commit your code
3.push it to heroku master
- Run
heroku run python manage.py makemigrations,,,, heroku run python manage.py migrate
Your issue should be solved
Solution 3:[3]
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:
- create db.sqlite3 file on heroku under your app directory
- run "python manage.py migrate" locally to create the db.sqlite3 and push it to the heroku repo
Solution 4:[4]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dishant Chavda |
Solution 2 | anaco ndeda |
Solution 3 | ac_baz |
Solution 4 | Kennedy Lucas |