scene
-
Suppose you have a project A
-
You deployed project A before, which contains tables table1 and table2 of two databases, and they were successfully deployed
-
Then you add some functions, so another table table3 is created
-
So when deploying again, it is found that table3 cannot be created successfully even after executing the makemigrations and migrate commands, but table3 can be seen in the admin interface of django, as follows:
-
The userinfo here is the table3 I mentioned, it can be seen in the admin interface, but you just can’t operate it
-
The error message is as follows:
ProgrammingError at /admin/nnsh_backend_new/userinfo/
relation "nnsh_backend_new_userinfo" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "nnsh_backend_new_userinfo...
^
Request Method: GET
Request URL: http://nus-nnsh-backend-new-1fec143342b2.herokuapp.com/admin/nnsh_backend_new/userinfo/
Django Version: 4.0.1
Exception Type: ProgrammingError
Exception Value:
relation "nnsh_backend_new_userinfo" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "nnsh_backend_new_userinfo...
^
Exception Location: /app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py, line 85, in _execute
Python Executable: /app/.heroku/python/bin/python
Python Version: 3.9.16
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: Sun, 16 Jul 2023 09:51:46 +0000
reason
-
There are a number of database conflicts that have occurred, which can be as follows, but are much more than:
- There are some more fields in a certain table in your modified version.
- But if you don’t have these fields in your existing information, this will actually cause conflicts, and the result of such data migration will fail
-
One of the most brutal methods, which is also the method I use (because my data is still in the testing stage, so it is not important), is to directly delete the project database on Heroku, and then re-makemigrations and migrate
operate
In your local terminal:
-
Log in to the heroku remote account:
heroku login
-
Reset the project database:
heroku pg:reset DATABASE_URL --confirm YOUR_APP_NAME
- If your project has only one database, then here
DATABASE_URL
can remain unchanged, if there are multiple databases, you need to specify exactlyDATABASE_URL
what is here YOUR_APP_NAME
Be sure to replace it with your own Heroku project name
- If your project has only one database, then here
-
Next set up the migration action manually or in the Procfile :
manual
heroku run python manage.py makemigrations YOUR_PROJECT_NAME
heroku run python manage.py migrate
- Note here
YOUR_PROJECT_NAME
is not the aboveYOUR_APP_NAME
; this is the name of your local project folder
automatic
- In the first line of the Procfile file write
release: python manage.py makemigrations YOUR_PROJECT_NAME && python manage.py migrate
- Then restart
git push
your heroku project and the migration will be complete