Using South migrations with Heroku - python

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.

Related

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 Heroku and postgresql

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

Django 1.8 migrations not obeying routers in multi db setup

I have a django project with migrated and unmigrated apps. I can be able to selectively migrate the migrated applications by running the command :
migrate <app> --database <database>
However I can't do this for unmigrated applications which are thirdparty.
To be able to sync them I have to run the command without the specifying app
migrate --database <database>
This though ends up with errors from other apps to be routed to different databases complaining that no table is synced.
Is this there anything I can do to resolve the issue.
I appreciate the effort to answer this question.
I guess you can try
python manage.py migrate 'nameoftheapp'
In fact django keep track of the migrations scripts already done in your DB. To check if the list of really applied and non applied migrations run:
python manage.py showmigrations --list
You will see a big X next to the migrations that have already been applied.
If the thirdparty migrations scripts are already applied just delete them from the table django_migrations in you database (created automatically by Django). And apply your migrations again:
migrate <app> --database <database>

Django, reset South migrations

Recently, I moved from Linux Mint to Ubuntu, uploaded my project's code to Github, downloaded it again and tried to continue developing. But I have a problem with South:
I tried migrating the apps(many apps) normally:
manage.py schemamigration apps --auto,
But I got:
"You cannot use --auto on an app with no migrations. Try --initial."
Then I tried '--initial',
But when I migrated the apps '$ ./manage.py migrate apps', I got:
"These migrations are in the database but not on the disk:
...(all the migrations I had, not sure how they ended up there)
I'm not trusting myself; either fix this yourself by fiddling
with the south_migrationhistory table, or pass --delete-ghost-migrations
to South to have it delete ALL of these records (this may not be good)."
I don't care about keeping the migrations so I tried python manage.py --delete-ghost-migrations, but I got "Unknown command".
Then I tried reseting migrations the way this post recommends, so I did:
$ python manage.py reset south
But I got "Unknown command" again.
¿How can I fix this so I can keep working on my project? Sorry if it is something obvious.
*When I was working on Linux Mint I used Mysql, now on Ubuntu I intalled Postgre. This probably doesn't have anything to do with the error cause I think I got it right and configured it correctly with my django project. But maybe you should know it if the solution needs some database manipulation. Thanks in advance.
Use the following command to clear the database:
./manage.py sqlclear south | ./manage.py dbshell
then make sure to remove the migrations directory in the app to fully reset south:
rm [app_name]/migrations

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