简体   繁体   中英

Django migrations: relation does not exist

I have a working site and database on my dev server which I am trying to setup on a live server. I am doing the following:

  • Git clone the repository from the dev server to the live server
  • Create empty database on live server (Posgres)
  • Update settings.py on the live server for all the relevant database etc settings
  • delete all migration files/folders and delete all *.pyc files
  • python manage.py makemigrations

I get the error: django.db.utils.ProgrammingError: relation "myapp_mytable" does not exist .

I can't seem to get the initial migration to happen. The only solution I have found is to go into my settings.py file and comment out all my apps within INSTALLED_APPS and go into my main urls.py file and comment out all my urls.

After commenting out those sections, I am able to do the initial migration. After that I can then uncomment my apps and start migrating them one by one, ie: python manage.py makemigrations appname then python manage.py migrate

So I have a workaround but it is far from ideal. Surely there is a way to tell django that I have created a brand new empty database so it needs to do the initial migration first. I am automating the server setup with Ansible, so requiring me to do all of this manual comment/uncomment and multiple migrations is not good.

UPDATE:

As per the comments, I am not suppose to delete the migrations. So I did the following on the dev server to try and recreate them: link . However even though I now have migration files which I have copied to the live server, when I try run them I get the same error as above.

I have read through the migration files and I don't see anything that mentions creating the initial migration / database schema. It only mentions creating my app models. I can't seem to figure out how to do the initial migration before the app migrations can be done. I need to somehow recreate all the migration files (including creating the initial db schema) from scratch so they can be run on the server.

I eventually figured out the issue thanks to @Daniel Roseman in the comments. I was doing the following:

list(Correlation().get_entries())

What this was doing was creating a model instance (ie: Correlation) and calling the get_entries() method against that. I then surrounded that in list(). For some reason, that was stopping the migrations from working. I removed the list() which wasn't necessary anyway, and it all works now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM