Edit Django files on Heroku without editing my database files - django

I wanna make some changes to my Django site running on Heroku, how can i upload my new changes with git push without touching my database, so i don't loose any data?

You would typically have your database on a seperate dyno on heroku. For e.g. you might have configured heroku-postgresql to keep your DB. When you git push to heroku it only moves your application to the heroku server, and doesn't overwrite the database, i.e. unless you're using some DB like sqlite3 which might be saving the data in a local file within the application directory.

Related

How to stop the data base from being updated in production

I am new to Heroku, I have deployed a site from git but the issue is that every time I update the site code, the database also gets updated and all data from my localhost gets in the production, and the data of users from the site is lost. I don't use Heroku ClI and just have simply connected GitHub to Heroku. It's a Django app with a sqlite3 database. I tried .gitignore and git rm --cached too but didn't work. Please tell a way to stop the database file from getting updated when I push it.

How to completely reset Postgres database on Heroku?

I have a learning project deployed on Heroku. It had a Postgres database provisioned. I introduced some major changes in the models of my Django project and destroyed the old database and provisioned a new one, which is totally empty, but it is not working like an empty database.
When I run the command heroku run python manage.py makemigrations, I get the error message
You are trying to add a non-nullable field....
Why am I getting this message when I have destroyed the old database?
First of all, you should never run manage.py makemigrations on Heroku.
By the time your code gets there no model changes should exist to generate new migrations. Run makemigrations locally to create migration files. Run migrate locally and on Heroku to apply migrations to your database.
Now that that's out of the way, this is likely caused by existing migrations files, not anything in your database. If you truly want to start over you can delete the files from each of yours apps' migrations/ directories.
Finally, there is no need to destroy and reprovision your database to reset it. Instead you can use heroku pg:reset:
The PostgreSQL user your database is assigned doesn’t have permission to create or drop databases. To drop and recreate your database use pg:reset.
use this command
heroku pg:reset

How to connect with postgresql database after deployment to Heroku?

I'm new to Django but I'm deploying a Django-based website to Heroku, using Postgresql. The deployment was successful, and the website is online and has established connection with the database. However, none of the data from my local database has migrated to the heroku database, causing it to be a blank database. If I go into the admin section and manually input a datapoint, it appears on my site, so I know that database is correctly serving data. What is the proper way for migrating data from your local database to your online, heroku version of the database? I thought the following code would migrate the data:
heroku run python manage.py makemigrations
heroku run python manage.py migrate
But apparently I'm missing something.
make migrations will create a migration that contains your schema, but no data. The migrate command applies the migration to the database.
In order to provide data to be sent over as part of the migrate command you need to either create a data migration or use a fixture.
Another option you have is to dump your local database and do an import into Heroku Postgres
All in all, it depends on how much local data you have that you want copied over. If its only a few rows, I would use either a data migration or a fixture, if its 100s or 1000s of rows an export/import of your dataset is your best bet.

How to keep changes made on sqlite database

I have a Django app with a SQLite database. This app is deployed on Heroku.
When someone uses the app and add data into the database, the sqlite database is modified. But when I make a code change on Github and then deploy this new version of my app, I also deploy the sqlite database (which doesn't have the new changes made by the users) and so I remove the changes.
What is the best process to prevent this ?
The solution is simple: you must not use sqlite on Heroku. As you have discovered, the file system on Heroku is ephemeral, and changes aren't persisted between deploys. Plus, if you scaled to more than one dyno, they wouldn't share the same database.
Use the Postgres add-on instead.
I don't know much about Heroku but if there's a seperate copy of your databse files on Heroku then you should add your sqlite files to the .gitignore file. This way you sqlite files won't be sent to Heroku along with the rest of the code. Also you'll need to delete the sqlite files from your repo which have already been added. Search more about gitignore. A general .gitignore for Django projects contains the following -
*.pyc
__pycache__
myvenv
db.sqlite3
.DS_Store
settings.py
Just copy this to your existing gitignore or make a new if not already present. A gigignore file is usually present in the root directory of the project.

Why is django manage.py syncdb failing to create new columns on my development server?

I am trying to create a development server from a production server from which I can test out new ideas.
I created a duplicate of my production server's database by dumping it using Postgres' db_dump and then imported the dump into a new database.
I then copied my production django directory and altered all .py files to refer to server_debug. rather than server in my import statements.
Using the admin interface to alter some data works in that only the development server has its data altered.
However, when I then try adding a new field in my models.py in my development server, manage.py syncdb fails to create it.
Is there something I am neglecting that could cause manage.py to refer to my production server rather than my development server?
syncdb doesn't touch tables that already exist. You need to either reset the app (easiest if you don't care about the data), modify the table manually (more of a quick hack) or use a migration app and version your models — South, for example.