Django Heroku and postgresql - python

I am new to publishing django apps on heroku.
I have read their tutorials and figured I would start simple by modifying their template django project.
So far everything was good.
Then I made an app as one does, made a model and ran
python3 manage.py makemigrations my_app
python3 manage.py migrate
and things seemed ok.
I then pushed to heroku and there were no immediate complaints.
However, now when I try to make save a new model I get the error:
ProgrammingError at /my_app/
relation "my_app_myappmodel" does not exist
LINE 1: INSERT INTO "my_app_myappmodel" ("field1", "field2", "field3") VALUES...
odd...
So I run this locally and everything works fine.
I have tried cleaning my migrations, faking my migrations, squashing my migrations, etc (as other S.O. posts suggest)
Nothing works.
What is up and how do I fix it?

You need to actually run the migrations on Heroku once you have pushed the code generated by makemigrations. You do this via heroku run manage.py migrate.

run the following command from your terminal
heroku run python manage.py migrate
or you can also do:
in your local settings.py, change your DATABASES variable to use the heroku one then run from the terminal
python manage.py makemigrations
python manage.py migrate
but you should not normally locally make changes to the heroku production database (as in option 2) except if you are really desperate or don't care

Related

django. The manage.py is not working after migrating mysql table

I install mysql as my database in my django project.
I did makemigration and migrate it on the django.
After that I wanted to try to run the server, but the python manage.py is not working at all even if there is python stored in my computer.
python_version_in_cmd
I have the screenshot here where it shows no response at all but pip is responding. django
This is my computer environment path computer enviroment path
When I was installing mysql the mysql/python was failing, is that the problem? any hints?
What is the error when running python manage.py runserver? Not knowing that, I could make some guesses.
Make sure your models are setup correctly.
After you get your models written, you can run python manage.py makemigrations
Then you need to run python manage.py migrate in order for the models to be applied to the database.
Some guidance can be found here: https://www.geeksforgeeks.org/django-migrations-python/

My django code is requesting a table that doesn't exist

I tried to delete the 'db.sqlite3' file and do the migration again, but it didn't work
If you made changes to your model, you need to run python manage.py makemigrations. If this is a new project that you have never deployed to production, you can delete the migrations folder before running this command.
Then run python manage.py migrate.

Django-Cms and Django-shop migration issues

I started creating my website on my own PC using the Django-cms install script then I tried to add a lot of applications such as Django-Helpdesk and others and everything was smooth until I tried to add pieces of djangoSHOP demo project and dependencies to email_auth.
Then I started to have report that the migration history was inconsistent and migrations applied out of order.
Before adding djangoShop to the installed apps, I just add the following commands to setup the DB:
manage.py migrate --run-syncdb --noinput
manage.py migrate --noinput
and everything was ok.
Now, as I'm just starting, I don't have changes to the schema to apply but most of the django apps I try to install have migrations folders with several migrations.
I don't want to update an existing schema, I want to create a new one.
I'm working from a blank database so do I really need to apply all those migrations ? To me it seems that the default behaviour of running migrations is not compatible with starting from a blank db,
I don't understand what to do just initialise the database to just have the required tables created.
Did anyone got the same kind of problems?
How did you fixed it?
Edit:
django-cms>=3.5,<3.6
Django 1.11.19
Error message:
Django InconsistentMigrationHistory: Migration Helpdesk.000xxxxx.py is applied before its dependency email_auth.0001_initial.py on database 'default'

Using South migrations with Heroku

I have successfully synced my database using south on the local server. I am having problems using south in Heroku. When I run
git add app/migrations/*
git commit -m 'adding new migrations'
heroku run python manage.py migrate app
I am getting a DatabaseError. Relation field already exists.
Any ideas why this isn't working? Also, do I need to run migrations locally and on the production environment each time one of my models change? Thanks for reading.
it seems you already have the fields in your database for that app. try faking the migrations by running
heroku run python manage.py migrate app --fake
do this on Heroku:
heroku run python manage.py migrate YOUR_APP_NAME 0001 --fake
then
heroku run python manage.py migrate YOUR_APP
that solved it for me. Just running migrate would give errors as for some reason south would try to run the initial migration as well. So I faked that one, and running the first actual migration (0002) worked fine after.

How to make South works in Heroku for a Django app

I am working on Python/Django and I am trying to use South to manage my database. In local environment is working great. The problem comes when I deploy to Heroku. The issue is that when I create a migration with
$heroku run manage.py schemamigration mydjangoapp
It looks like it works (the shell confirmed it), however, then I try to use migrations and it won't work. When I do:
$heroku run python manage.py migrate mydjangoapp
I get this;
The app 'createtuto' does not appear to use migrations
I checked on the problem and it looks like heroku doesn't allow South to create the migration directory at /myDjangoapp/migrations.
Is there anything I can do to make it work?
I tried to use convert_to_south, but I got the same results: At the beginning it looks like it worked, but It did not, not migration created.
When you run 'heroku run' it connects to an isolated instance of your deployed environment. It does create the migration, however that migration is not contained within your slug. Each time you do a 'git push heroku master' it installs your dependencies and packages your application into a slug. This is more or less a tarball of your application which enables Heroku to easily deploy it to new dynos as you scale up.
In order to run a migration on Heroku you would create the migration locally, check it in, then run the migration on heroku. Something similar to:
manage.py schemamigration mydjangoapp
git add mydjangoapp/migrations/*
git commit -m 'adding new migrations'
git push heroku master
heroku run python manage.py migrate mydjangoapp
I follow the direction from Mike Ball here successfully:
http://www.mikeball.us/blog/using-south-on-heroku/
Like CraigKerstiens answer said, you should make the migration locally first then push to heroku. Before you make a migration on Heroku, make sure you convert your Heroku instance into south, for example
heroku run bin/python django_project/manage.py convert_to_south django_app

Categories