Migrate databases from local django to heroku - django

I have an application in Django that I deploy in heroku.
The deploy works well, but my model's database was not migrate.
After the deploy, I run again localy:
python manage.py makemigrations
python manage.py migrate
After, I do:
heroku run python manage.py makemigrations
heroku run python manage.py migrate
And after I run the server and wait it works well:
heroku run python manage.py runserver
I have 3 models:
cliente, categoria, produto
produto has a ForeignKey to categoria. So, localy, I have 3 databases:
produtos_produto, produtos_categoria, cliente.
I use PostgreSQL as database localy and in heroku.
But in heroku, I don't have any of this databases.
When I run the server, in browser I have the following answer:
ProgrammingError at /
relation "produtos_categoria" does not exist
LINE 1: ...ia"."descricao", "produtos_categoria"."logo" FROM "produtos_...
^
Request Method: GET
Request URL: http://redewebsite.herokuapp.com/
Django Version: 1.9.2
Exception Type: ProgrammingError
Exception Value:
relation "produtos_categoria" does not exist
LINE 1: ...ia"."descricao", "produtos_categoria"."logo" FROM "produtos_...
^
Exception Location: /app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py in execute, line 64
Python Executable: /app/.heroku/python/bin/python
Python Version: 2.7.11
Python Path:
['/app',
'/app/.heroku/python/bin',
'/app/.heroku/python/lib/python2.7/site-packages/setuptools-19.6-py2.7.egg',
'/app/.heroku/python/lib/python2.7/site-packages/pip-8.0.2-py2.7.egg',
'/app',
'/app/.heroku/python/lib/python27.zip',
'/app/.heroku/python/lib/python2.7',
'/app/.heroku/python/lib/python2.7/plat-linux2',
'/app/.heroku/python/lib/python2.7/lib-tk',
'/app/.heroku/python/lib/python2.7/lib-old',
'/app/.heroku/python/lib/python2.7/lib-dynload',
'/app/.heroku/python/lib/python2.7/site-packages']
Server time: Sex, 4 Mar 2016 17:50:43 +0000

You must run makemigrations locally, then commit those generated migration files to git. Heroku will run then automatically when you deploy.

Related

Programming Error in deploying Django website on Heroku

I am learning Django and wanted to deploy the app on Heroku. upon following the Heroku documentation on deploying the app to the platform. It is throwing errors.
I have 2 apps in the project:
blog (for writing the blogs)
userAuthentication (user auth)
Please help!
ERROR on Heroku deployed app:
ProgrammingError at /
relation "blog_categories" does not exist
LINE 1: ...categories"."name", "blog_categories"."name" FROM "blog_cate...
^
Request Method: GET
Request URL: https://sciencecreed.herokuapp.com/
Django Version: 3.2
Exception Type: ProgrammingError
Exception Value:
relation "blog_categories" does not exist
LINE 1: ...categories"."name", "blog_categories"."name" FROM "blog_cate...
^
Exception Location: /app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py, line 84, in _execute
Python Executable: /app/.heroku/python/bin/python
Python Version: 3.9.4
Python Path:
['/app/.heroku/python/bin',
'/app',
'/app/.heroku/python/lib/python39.zip',
'/app/.heroku/python/lib/python3.9',
'/app/.heroku/python/lib/python3.9/lib-dynload',
'/app/.heroku/python/lib/python3.9/site-packages']
Server time: Thu, 29 Apr 2021 21:36:58 +0000
ERROR on executing "heroku logs"
The migrations work fine when running them locally with sqlite3 database but not on Heroku with PostgreSQL.
I tried running python manage.py makemigrations & python manage.py migrate on Heroku but no luck.
You can visit https://sciencecreed.herokuapp.com/ for the complete info of the error.

Django project on Heroku initial data fixture integrityerror

I have deployed my project to Heroku and currently trying to load the data dump from local sqlite database to the Heroku database. The remote database is clean and untouched other than the initial migrate command.
I have tried the following combinations of dump but all of them returned an error
python manage.py dumpdata --exclude contenttypes --> data.json
python manage.py dumpdata --exclude auth.permission --exclude contenttypes --indent 2 > data.json
python manage.py dumpdata --exclude auth.permission --exclude contenttypes --exclude auth.user --indent 2 > data.json
and the error is:
django.db.utils.IntegrityError: Problem installing fixture
'/app/data.json': Could not load wellsurfer.Profile(pk=6): duplicate
key value violates unique constraint "wellsurfer_profile_user_id_key"
DETAIL: Key (user_id)=(1) already exists.
i would like to post the json file here but it is about 120,000 lines. But i can provide specific portions if needed. The error clearly says the key exists but the database is clean in the beginning. Obviously, i am doing something very basic thing wrong and i hope you can point me in the right direction. I have tried recommendations that i found on Stackoverflow with no success. How to manage.py loaddata in Django
I had the same problem, and this is what worked for me
source (local sqlite)
python manage.py dumpdata --natural-foreign --indent 4 > datadump.json
(this will include everything, even the auth app / users)
destination (heroku postgres)
python manage.py migrate
python manage.py shell
>>> from django.contrib.contenttypes.models import ContentType
>>> ContentType.objects.all().delete()
>>> quit()
Finally, run following command to load the json data:
python manage.py loaddata datadump.json

Manage.py migrate fails in this Heroku python "Get Started Guide" at Step 14 "Provision a Database"

Here's the link to the guide:
Getting Started on Heroku with Python
The guide says to run: heroku run python manage.py migrate which produces the following output:
... Your models have changes that are not yet reflected in a migration, and so won't be applied. Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
So then I run
>heroku run python manage.py makemigrations and get:
Running python manage.py makemigrations on ⬢ sleepy-reef-12488... up, run.5322 (Free)
Migrations for 'hello':
hello/migrations/0002_auto_20200125_2146.py
- Alter field when on greeting
Then re-running the first code: heroku run python manage.py migrate produces the same message as before.
The end result is this template error at: my test app
TemplateSyntaxError at /db/
'staticfiles' is not a registered tag library. Must be one of:
admin_list
admin_modify
admin_urls
cache
i18n
l10n
log
static
tz
Request Method: GET
Request URL: https://sleepy-reef-12488.herokuapp.com/db/
Django Version: 3.0.2
Exception Type: TemplateSyntaxError
Exception Value:
'staticfiles' is not a registered tag library. Must be one of:
admin_list
admin_modify
admin_urls
cache
i18n
l10n
log
static
tz
Exception Location: /app/.heroku/python/lib/python3.7/site-packages/django/template/defaulttags.py in find_library, line 1025
Python Executable: /app/.heroku/python/bin/python
Python Version: 3.7.3
Python Path:
['/app/.heroku/python/bin',
'/app',
'/app/.heroku/python/lib/python37.zip',
'/app/.heroku/python/lib/python3.7',
'/app/.heroku/python/lib/python3.7/lib-dynload',
'/app/.heroku/python/lib/python3.7/site-packages']
Server time: Sat, 25 Jan 2020 21:46:47 +0000
The answer is here:
'staticfiles' is not a valid tag library: Template library staticfiles not found
I replaced
% load staticfiles %
with
% load static %
in a couple of files and now my app works. I can't tell you why this works. I wish I could. I'm new to Django.

Django error, constantly faced with the same server error even if I start an entirely new application and run it

OperationalError at /
no such table: core_document
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 2.1.4
Exception Type: OperationalError
Exception Value:
no such table: core_document
Exception Location: C:\Python37-32\lib\site-packages\django\db\backends\sqlite3\base.py in execute, line 296
Python Executable: C:\Python37-32\python.exe
Python Version: 3.7.1
Python Path:
['E:\Django\Dec19\simple-file-upload-master',
'C:\Python37-32\python37.zip',
'C:\Python37-32\DLLs',
'C:\Python37-32\lib',
'C:\Python37-32',
'C:\Python37-32\lib\site-packages']
Server time: Wed, 19 Dec 2018 04:22:57 +0000
Try to run migrations:
python manage.py makemigrations
python manage.py migrate
The first line of the error explains the core issue:
OperationalError at / no such table: core_document
The table which is being referenced doesn't exist. This usually happens when migrations haven't successfully run. To begin troubleshooting (and to possibly fix the problem entirely), run the Django migrations:
python manage.py makemigrations
python manage.py migrate

ImportError at /admin/ No module named polls_ChoiceField.urls

Django 1.6.2
MacOSX 10.9.2
Python 2.7
I recently deleted a Django App "polls_ChoiceField" which was sitting inside another app "polls" as I was only using it to test a few things. However since i deleted it then the app it was sitting in no longer works.
I deleted the file through the pyDev package explorer. When I synced the Database it gave my the option of deleting the stale content, I selected yes.
Can anyone tell me what I have to do to get the original "polls" all running again?
Relevant Terminal output
localhost:src brendan$ python manage.py syncdb
Creating tables ...
The following content types are stale and need to be deleted:
polls | choice_choicefield
polls | poll_choicefield
Any objects related to these content types by a foreign key will also
be deleted. Are you sure you want to delete these content types?
If you're unsure, answer 'no'.
Type 'yes' to continue, or 'no' to cancel: yes
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
I then synced the DB
localhost:src brendan$ python manage.py syncdb
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
And I am able to run the server
localhost:src brendan$ python manage.py runserver
Validating models...
0 errors found
When i try to revisit the app in my browser i get
ImportError at /polls/
No module named polls_ChoiceField.urls
Request Method: GET
Request URL: http://localhost:8000/polls/
Django Version: 1.6.2
Exception Type: ImportError
Exception Value:
No module named polls_ChoiceField.urls
Exception Location: /Library/Python/2.7/site-packages/django/utils/importlib.py in import_module, line 40
Python Executable: /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Python Version: 2.7.6
Python Path:
['/Users/brendan/Dropbox/workspace/bias_experiment/src',
'/Library/Python/2.7/site-packages/distribute-0.7.3-py2.7.egg',
'/Library/Python/2.7/site-packages/setuptools-2.2-py2.7.egg',
'/Library/Python/2.7/site-packages/pip-1.5.4-py2.7.egg',
'/Library/Python/2.7/site-packages/yolk-0.4.3-py2.7.egg',
'/Library/Python/2.7/site-packages/virtualenv-1.11.4-py2.7.egg',
'/Library/Python/2.7/site-packages/virtualenvwrapper-4.2-py2.7.egg',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages',
'/Library/Python/2.7/site-packages']
Server time: Mon, 28 Apr 2014 16:26:46 +0100
Thanks
EDIT:
My urls.py with the bad line of code (the middle url)
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^polls_ChoiceField/', include('polls_ChoiceField.urls', namespace="polls_ChoiceField")),
url(r'^admin/', include(admin.site.urls)),
)
Thanks again.
Check your project-level urls.py, looks like it still tries to use urls from the deleted app.