3

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.

3
  • To be clear: What you're hoping for is that your local machine's db and the remote heroku machine's db are always in sync? This would be called "database synchronization" and the reason you aren't finding a simple script for it is that it's a hard problem. For example, what do you want to happen if your local machine and the heroku machine cannot communicate (loss of internet for example) when a change is made in one location or the other. What if there are conflicts? A common model for this is to have a master and a slave, but there are other approaches. Commented Nov 18, 2019 at 6:03
  • You can use git push heroku command. Initially push your code to Git version control system and follow the heroku documentation. The doc link is here: devcenter.heroku.com/articles/… Commented Nov 18, 2019 at 6:12
  • @jchung Not always in sync, just when I run a command. I've found an answer and wrote it below, though. Thanks for introducing me to the topic of database synchronization. Side note: the reason I don't need constant syncing is because it's just me making instances and writing their field values, so I'd know to pull from heroku to local before instantiating and writing values locally, and vice versa. Commented Nov 18, 2019 at 11:50

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

For a one-time push and a small dataset this works totally fine. Good work finding it :) A continuous sync of objects in two directions is hard and far more complex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.