2

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.

Community
  • 1
  • 1
darkpool
  • 13,822
  • 16
  • 54
  • 89
  • 2
    *Why* would you delete all the migration files? The whole point is not to do that but to run the ones you have already created. – Daniel Roseman Feb 13 '17 at 10:10
  • 1
    Ok, I obviously misunderstood how to go about this. I was under the impression that I needed to run makemigrations on the new server so that django would see it is an empty database and create new ones. Obviously I misunderstood. Not sure why a perfectly valid question should be downvoted. What is the point of asking a question then. Original question has been updated. – darkpool Feb 13 '17 at 10:44
  • Since your code is in git, you should just reset the state to where the migrations are, rather than recreating them. And I'm not sure what you mean by "how to do the initial migration before the app migrations" - there is no separate "initial migration". – Daniel Roseman Feb 13 '17 at 11:28
  • What I meant by 'initial migration' is because even though I am now running manage.py migrate with the correct migration files, I still get the same error saying relation does not exist. The database is empty, so of course the relation does not exist yet. Im not sure why django isnt creating all the required models. – darkpool Feb 13 '17 at 11:33
  • 2
    It seems likely that you have an import somewhere that is causing a database query to be made on process startup. You should post the full traceback to help track down the issue. – Daniel Roseman Feb 13 '17 at 11:35
  • Excellent, that was the issue. This statement was the issue: list(Correlation().get_entries()). That was just a 1 liner to create a model instance and call a method on it and create a list from that. Not sure why I was doing that, it served no purpose and my site works fine without the list() part. The migrations are working now. If you add an answer to the original question, I will accept it so the question can be closed. – darkpool Feb 13 '17 at 12:08

1 Answers1

3

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.

darkpool
  • 13,822
  • 16
  • 54
  • 89