menu

Questions & Answers

How heroku run python manage.py migrate?

I deployed a simple Django app in Heroku

Steps:

- git push heroku master

- heroku run python manage.py makemigrations ( or + app_name)

it seem to affect:

  0002_main.py:
- Create model Comment
- Remove field status from match
- Remove field quantity from slot
- Add field avatar to account
- Add field slots to match
- Alter field verification_code on account
- Alter field verification_code on slot
- Add field match_object to comment
- Add field user to comment
- Alter index_together for comment (1 constraint(s))

then I run

- heroku run python manage.py migrate

but i received:

  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.
Answers(5) :

The Heroku filesystem is read-only as per the documentation.

This means that when you disconnect from the dyno the files created by the makemigrations command will be destroyed.

To solve your issue you can:

  1. Commit your migration files to Github(or your source control system) and then run the migrate command on the Heroku shell - recommended
  2. Create the migration files and then run the migration on the heroku bash shell. - NOT RECOMMENDED on production

Your migration files should be committed to your source control, and never run makemigrations on heroku.

With committed migration files, this problem becomes non existant.

Comments:
2023-01-19 00:55:12
that right, I missed the migration files, I just ran makemigrations on heroku
2023-01-19 00:55:12
But what if we wanted to do so. e.g, if we have altered a model of an app which is from an installed package and we want to show that on the server, as we don't send the packages but just a requirements.txt file

Make sure you have committed migrations file. then run

heroku run python manage.py migrate

You can specify the app name in the following way:

heroku run python manage.py migrate -a <app-name>

Please see this documentation.

To make Heroku handle applying the migrations when deploying add the following to you Procfile

release: python manage.py migrate

Either

heroku run python manage.py migrate

or

heroku run python3 manage.py migrate

If needed to specify app name:

heroku run python3 manage.py migrate -a <app-name>

Comments:
2023-01-19 00:55:12
hm good one, trying to run python manage.py migrate from the ssh connection (directly on the VM) results in 5432 port error, thanks