When local Django deploys Heroku, some/some databases always fail to create a successful relation "nnsh_backend_new_userinfo" does not exist LINE

Article Directory

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:
    insert image description here

  • 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_URLcan remain unchanged, if there are multiple databases, you need to specify exactly DATABASE_URLwhat is here
    • YOUR_APP_NAMEBe sure to replace it with your own Heroku project name
  • 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_NAMEis not the above YOUR_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 pushyour heroku project and the migration will be complete

Guess you like

Origin blog.csdn.net/qq_42902997/article/details/131753285