Django data was not brought to heroku - django

I have Django apps, which include users and posts models. The apps work perfectly at local following making migrations and migrating. I tried two different ways to deploy the apps to Heroku. First, using Heroku Git, the apps works on Heroku the same as at local.
When using GitHub, however, all data were not brought to Heroku. I tried to run "python manage.py makemigrations" and "python manage.py migrate" on Heroku, but the data from local was not brought still.
I would really appreciate it if you could please explain and help with the issue using GitHub.

I think you have to migrate on Heroku as well, try:
heroku run python manage.py migrate
See the heroku docs

check your .gitignore file maybe db.sqlite in gitignore file and you can remove it
another option is dumpdata and load in production
you can run
python3 manage.py dumpdata > data.json
and load data in production
python3 manage.py loaddata data.json

Related

Heroku push does not upload migrations

I have developed a Django project and want to deploy it on a Heroku server. The problem is that my migrations are not uploaded after 'heroku push' command.
After I run
python ./Code/manage.py makemigrations
python ./Code/manage.py migrate
I see my migration files and database locally. But, after that, when I push them to Heroku, they seem not to be there. My website on Heroku gives the error that some tables are not available and when I run bash on the server to see my files there aren't any migration files and the migration folders in my apps only have the init.py file.
I even tried to make the migrations and migrate on the release phase. This is the Procfile:
release: bash ./release_tasks.sh
web: gunicorn --pythonpath Code Code.wsgi --log-file -
and this is release_tasks.sh:
python ./Code/manage.py makemigrations
python ./Code/manage.py migrate
Again, when I push to Heroku everything is ok and I see the correct migration messages, which shows that migration was successfully carried out. But still, there is no migration file on the server and my website gives the same error.
Here is what I don't understand:
screenshot
I have migrations locally and Git is up-to-date, but I don't have them on the server.
As with any code you create locally, you need to add it to git and then commit.
./Code/manage.py makemigrations
git add ....
git commit
git push origin heroku
You should remove the makemigrations step from the release tasks, there is no point running that on Heroku.

Django on Heroku - ProgrammingError at / relation "..." does not exist

I'm getting this error. I know you usually get this error because the databases wasn't properly migrated.
When I run heroku local web, the website works fine when I go to localhost:5000.
However after I deploy the app to heroku with git push heroku master, the error comes up.
In other words, it works in my local environment. But it does not work after deploying to heroku.
I have Heroku-Postgres installed as an add-on in heroku.
What could be causing this?
excute migrations and makemigrations in bash heroku. open the terminal in the local project folder and give the following commands:
heroku run bash
~$ ./manage.py makemigrations
~$ ./manage.py migrate
~$ exit
The following steps did it for me
Make all the necessary migrations (python manage.py makemigrations) and migrate (python manage.py migrate) locally,
push to heroku master
and finally running heroku run python manage.py migrate
Solved the issue
I experienced the same error after making a change to a model and then deploying this change to heroku.
The only way I managed to fix this problem was to do the following:
Reset the database in heroku
Delete the migrations files from the migrations folder for the broken app locally (but keep the directory and the __init__.py file)
Run python manage.py makemigrations and python manage.py migrate. This will repopulate the migrations folder with clean migration files.
Push changes to master (ensure that you do not have migrations directories in .gitignore files.
Deploy changes to heroku
Run the heroku shell heroku run bash
Run python manage.py migrate
I was able to do this because my tables did not have much data in, but I would try to avoid resetting the database if I had more data in my tables.

Deploying Django app to Heroku via CircleCI: How to migrate database?

How can I run python manage.py makemigrations and python manage.py migrate automatically when deploying a Django app to Heroku via CircleCI. It seems all commands run local to CircleCI, but not on the deployed application in production.
Is there a way?
python manage.py makemigrations should be running locally, and you may commit the migration files along with the code.
Regaridng the migrations, open your Procfile and add this line: release: python manage.py migrate.
This will tell Heroku to migrate before deploying a new version of your code.

problems updating DB to heroku with south migrations

I have a Django proyect running in heroku for some time now, the thing is that, tree days ago, I've tryed to update my schema model but, every time I write
heroku run python manage.py migrate quizzer
heroku keeps telling me that everything's up to date, but I've changed my models.py folder and run schema migration as always.
If you know why this is happening or how can I force a schema migration to my heroku app please tell me how.
Ps: I cannot delete the hole database as the data stored in heroku and the data stored in my local server database are not the same, and I don't want to loose the data of my users
Here is a workflow for running a schemamigration on quizzer after modifying your models.py
./manage.py schemamigration quizzer --auto # create migration
./manage.py migrate quizzer # apply migration locally
git add .
git commit -m "Changed quizzer models, added schemamigration"
git push heroku
heroku run python manage.py migrate quizzer # apply migration on heroku
It sounds like you might have forgotten to check your migration file (usually found in appname/migrations) into git, commit it and push it to heroku.
I had this problem too. I solved this by running heroku restart and running the migrate command again. Don't know why it works (suspect it has to do with initial), but at least it works.
Hope that helps!
South might be missing from requirements.txt. Try:
pip freeze > requirements.txt
...followed by another git add/commit/push.
Also, according to the South installation instructions, syncdb must be run first, "to make the South migration-tracking tables". So try:
heroku run python manage.py syncdb
...then try the migrate command again.

Django: no such table: django_session

I have found several topics with this title, but none of their solutions worked for me. I have two Django sites running on my server, both through Apache using different virtualhosts on two ports fed by my Nginx frontend (using for static files). One site uses MySql and runs just fine. The other uses Sqlite3 and gets the error in the title.
I downloaded a copy of sqlite.exe and looked at the mysite.sqlite3 (SQLite database in this directory) file and there is indeed a django_session table with valid data in it. I have the sqlite.exe in my system32 as well as the site-packages folder in my Python path.
Here is a section of my settings.py file:
MANAGERS = ADMINS
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'mysite.sqlite3', # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
I did use the python manage.py syncdb with no errors and just a "No Fixtures" comment.
Does anyone have any ideas what else might be going on here? I'm considering just transferring everything over to my old pal MySql and just ignoring Sqlite, as really it's always given me some kind of trouble. I was only using it for the benefit of knowing it anyway. I have no overwhelming reason why I should use it. But again, just for my edification does anyone know what this problem is? I don't like to give up.
After made any changes in code, run the following commands
manage.py makemigrations
manage.py migrate
it worked for me.
It could be that the server uses a different working directory than the manage.py command. Since you provide a relative path to the sqlite database, it is created in the working directory. Try it with an absolute path, e.g.:
'NAME': '/tmp/mysite.sqlite3',
Remember that you have to either run ./manage.py syncdb again or copy your current database with the existing tables to /tmp.
If it resolves the error message, you can look for a better place than /tmp :-)
run this in command shell:
python manage.py migrate
This fixed for me.
In case it helps anyone else: the problem for me was that I didn't have the django.contrib.sessions app uncommented in my INSTALLED_APPS. Uncommenting it, and rerunning a syncdb did the trick.
In my case the problem was that I forgot to run manage.py syncdb after I made some changes. When I did that the problem was solved.
When I run "manage.py runserver". If I run when I my current path is not in project dir.(such as python /somefolder/somefolder2/currentprj/manage.py runserver) I'll got the problem like you. solve by cd to project directory before run command.
One other possible cause can come from using:
./manage.py testserver
And then visiting the admin interface. This won't work because testserver creates a completely separate database in memory. If you want to visit the admin interface you need to use runserver.
This can happen if there are pending session migrations.
You have 17 unapplied migration(s). Your project may not work properly
until you apply the migrations for app(s): admin, auth, contenttypes,
sessions.
You can use the following command to run the migrations:
python manage.py migrate
This will fix the issue.
It happens may be beacause of undone migrations
• Executes following commands:
python manage.py showmigrations
python manage.py migrate --fake your_app_name zero
python manage.py showmigrations
• IF you are running on local machine, then remove file named 0001.init.py from migrations folder in your app
• Executes following commands:
python manage.py makemigrations
python manage.py migrate
• then run django server:
python manage.py runserver
had the same issue, my resolution was to simply add 'django.contrib.comments' to INSTALLED_APPS and run ./manage.py syncdb again.
I had similar problem for admin management. After several checks, run "python manage.py migrate" without assign APP's name (get "Apply all migrations: .....), then runserver and up on the web. It worked. I hope this could help.
You have unapplied migrations. your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
python manage.py migrate This one worked for me.
Maybe you have some unmigrated files.
Run
Python manage.py makemigrations appname
python manage.py migrate appname
python manage.py runserver
But if the error still continue
The run python manage.py migrate
The run server
create a schema and add its name under NAME in 'databases'
run manage.py syncdb
I had made some changes in Model which was not migrated to db properly. Using the command
manage.py makemigrations
fixed my problem. I hope this will help someone.
syncdb is obsolete try python manage.py makemigrations and python manage.py migrate solved the problem ,and donot forget to add the app name is the installed app in settings.py
I had the same problem so I ran
heroku run ls
and found that the db.sqlite3 file was missing from the server.
In my case, it was because I exempted it by adding it in .gitignore file.
Maybe it is not getting the path of db. Just add this to your settings.py:
import os
PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
#modify your db NAME as below:
'NAME': os.path.join(PROJECT_PATH,'mysite.sqlite3'),
In my case I only focused on different project and did not migrate the main application.
so first:
manage.py makemigrations
manage.py migrate
and then to the project's:
manage.py makemigrations <my other project name>
manage.py migrate <my other project name>
Had this problem too. Restarting postgres and apache2 did it for me. Makes me wonder if there was some kind of sqlite process left over which wasn't removed until you fiddled with the files or something.
I had this issue in a different scenario. I am new to Django and cloned a repository from github to practice on it. The file db.sqlite3 was also copied. But there was no django_session in it. When I did
./manage.py showmigrations
.. I found out that there were some migrations. But the tables were missing in sqlite, as I never ran migrate. My issue was resolved when I ran the migrate command. Hope this helps django newbies like me.
./manage.py migrate
Add 'django.contrib.sessions', line in INSTALLED_APPS
Run below commands from django shell
python manage.py makemigrations #check for changes
python manage.py migrate #apply changes in DbSQLite
python manage.py syncdb #sync with database
django_session will appear in database with (session_key, session_data , expire_date)
In my case, I had to erase the 'sessions' entry in the django_migrations table and run makemigrations and migrate afterwards. That created the django_session table.
For me, it was that I updated settings.py, ran the migrations, but the systemd process was still using SQLite because I did not reload it. Doing systemctl restart service_name solved the problem.
And it may be a case you are getting this error because you forget to run query python manage.py migrate before creating super user
If you are tired of using makemigrations and migrate but the error is same no such table django_session. Then just have a look to you code somewhere or the other you using session or calling it. Just comment the code where you are using session and then run the command makemigrations and migrate respectively. It 100% solve your issue.The cause of this error is that you deleted your migrations folder and database file that is the reason you are getting this error.Feel free to ask if problem is not solve
This worked for me.
From https://docs.djangoproject.com/en/2.2/topics/http/sessions/
Using database-backed sessions -
If you want to use a database-backed session, you need to add 'django.contrib.sessions' to your INSTALLED_APPS setting.
Once you have configured your installation, run manage.py migrate to install the single database table that stores session data.
Please try the following command when you change on your code
manage.py makemigrations
manage.py migrate
Simply run this commands
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
it's simple just run the following command
python ./manage.py migrate
python ./manage.py makemigrations AppName