Django Migrations not updating Heroku PostgreSQL database - django

I've been stuck on an issue for a while where I have a model that I've created locally (class User(AbstractUser)), ran "python manage.py makemigrations" and "python manage.py migrate", which works and interacts correctly with the django server when run locally.
When I push this to heroku however, I receive an error response to certain api requests which essentially say "relation "app_user" does not exist". I assumed this was because somehow the postgreSQL db didn't receive the migrations when the app was deployed to heroku. After logging into the db I noticed that it had a few other tables, including "auth_user" (I assume this is a default django table), but not "app_user". I have done "heroku run bash" and "python manage.py migrate" in an attempt to migrate the changes to the postgreSQL db, but this doesn't have any effect.
I'm not sure if this will be useful but the postgreSQL server I'm using is the standard Heroku Postgres Hobby Dev add-on.
My questions are:
Should running "python manage.py migrate" on heroku update the postgreSQL database? If so, how does this work?
Given that the migrations are all on heroku, how can I manually update the postgreSQL with the migrations?

Fixed by deleting old heroku db and creating a new one. Unsure what exactly caused the issue above but I feel it may be because we deleted all migrations on the repo at some point in the past (to start from scratch), which meant the current db state was not applicable to migrations in the repo and so could not be applied. After doing this, running migrate on the remote server works perfectly.

Related

Will migrations that I make to a django app reflect on the DB that is present in Kubernetes?

I have a Django application and I'm using MariaDB as the database. Both of them have been deployed to a namespace on kubernetes. Now I want to add an additional field, so I made changes in the models.py file in the django app. These changes were made locally - I pulled the code from GIT and just made the changes locally. Normally to apply the changes, I have to run manage.py makemigrations and manage.py migrate and all the changes would've been reflected on to the DB if the DB was present locally.
So now my questions are
How can I apply the changes to MariaDb that is there on Kubernetes ?
Will running manage.py makemigrations and manage.py migrate locally and redeploying the django app to kubernetes solve this issue ?
TLDR;
If you don't require multiple replicas, then the simplest way to do it would be to run your migrations when the container starts.
If you require multiple replicas, then you'll have to get creative with jobs and init containers.There's a good article with more info on it here: https://andrewlock.net/deploying-asp-net-core-applications-to-kubernetes-part-7-running-database-migrations/

How to sync local Django sqlite3 data with Heroku's postgres database?

I have a Django website served by Heroku with a model storing data about projects that I've worked on in the past. I already ran makemigrations and migrate locally before pushing to heroku with git and running heroku run python3 manage.py migrate. So, my database models and fields are synced, but I'm asking about the field values. Whenever I update the value of a field for a model instance locally, I want it (the data) to sync with Heroku, and vice versa–sync values I've updated on Heroku in the admin panel with my local sqlite3 database. Is there a command for updating the values of the database itself, or am I missing something? Because I've looked all over the internet for the last hour for how to do this one thing and I couldn't find the command to do it.
Side note: I also want the command I'm looking for to sync newly created instances, not just data for existing model instances.
Ok, I've figured it out.
First step is to run python3 manage.py dumpdata --exclude contenttypes > data.json. This copies the local database data into a file called data.json (that's created by > also if it doesn't exist).
Next, git push to heroku and run heroku run python3 manage.py migrate for good measure.
Finally, heroku run python3 manage.py loaddata data.json. This translates the data loaded from the local sqlite3 database and loads it to the heroku postgre database. Unless the translating is done when you dump the data. Regardless, this synchronizes the heroku data with the local data.
I haven't tested synchronizing the local data with the heroku data, but I'm sure it'll work the same way: heroku run python3 manage.py dumpdata --exclude contenttypes > data.json and then git fetch from heroku (I've never fetched before to sync a directory with what's on github but it should be straightforward).
That's all there is to it. If I locally change the name of a project that I worked on, update the date it was last worked on, and write a few more paragraphs on the work process, and I don't want to redo all of that in the heroku shell, then I just synchronize by dumping the data, pushing it to heroku, and loading it there.

Problem running migrations in Production (i.e. Heroku platform)

I made some changes to the db (i.e. added on column). I can make and run migrations just fine in my local env, the problem is in Production. I have made the migrations and pushed them to my remote repo, but when I run the migrations in production, nothing happens. I can makemigrations in production but when I run "heroku run python manage.py migrate", the system says, that are no migrations to implement.
This is worth mentioning though, initially I ran "heroku run python manage.py migrate --fake" because I had a lot intermediary tables that threw the error "table xxxxxx already exist" everytime I tried to run a migrations, so that fixed that problem but I believe that the actual change that I wanted(i.e. adding a column to the db) to make, actually got migrated as fake migration. How can I fix this?

can't delete old south migrations from heroku

I made many changes in my django app and when working locally i reset the database, deleted the migration and reseted south, recreated everything all over and it worked fine.
when trying to do the same in Heroku,
after droping the database using:
heroku pg:reset DATABASE
and also reseting south using:
heroku run ./manage.py reset south
and then pushing the Django app when looking at the heroku migration list using:
heroku run ./manage.py migrate --list
i can still see all of the old imgration,
though they are empty-- no * in the ()
so even after doing
heroku run python ./manage.py migrate accounts --fake
i still get migration errors:
DatabaseError at /admin/accounts/userprofile/
****relation "accounts_userprofile" does not exist
LINE 1: SELECT COUNT(*) FROM "accounts_userprofile"****
what can i do to solve it? is there a wayy to remove old migrations in heroku and just do it simple like when working locally by starting all over again?
or is their any other solution?
--fake is used when the database tables already exists and you no longer need to actually migrate the real database. As the tables don't exist, you should just run migrate without --fake.

Django: Heroku migrations resulting in an error

I've been finishing up my first Django app and have run into a snag with migrations in Heroku. I migrated with South locally and then attempted to move those migrations to the database on Heroku.
When I ran:
heroku run stentorian/manage.py syncdb migrate report
I received the following error:
['/app/stentorian', '/app/.heroku/venv/lib/python2.7/site-packages/pip-1.1-py2.7.egg', '/app', '/app/.heroku/venv/lib/python27.zip', '/app/.heroku/venv/lib/python2.7', '/app/.heroku/venv/lib/python2.7/plat-linux2', '/app/.heroku/venv/lib/python2.7/lib-tk', '/app/.heroku/venv/lib/python2.7/lib-old', '/app/.heroku/venv/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7', '/usr/local/lib/python2.7/plat-linux2', '/usr/local/lib/python2.7/lib-tk', '/app/.heroku/venv/lib/python2.7/site-packages', '/app/.heroku/venv/lib/python2.7/site-packages/PIL']
Error: Command doesn't accept any arguments
I've researched this and can't seem to find how to resolve this. Prior to this, I installed the django-flaggit app to my application, which doesn't use migrations, and had to use a traditional syncdb to get the tables set up in Heroku. I'm wondering if this had an affect.
If anyone has any insight into this issue, it would be much appreciated.
What you are doing here is actully calling two different commands in a row: syncdb and migrate.
The usual workflow with south is to run syncdb to sync all your non-south-supported apps, and then migrate, to work on south-supported apps. Actually, if you just run syncdb with south installed, it will tell you which apps are unsynced and must be migrated.
So, to summarize:
heroku run stentorian/manage.py syncdb
heroku run stentorian/manage.py migrate report