简体   繁体   English

Django迁移:不存在关系

[英]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 Git将存储库从开发服务器克隆到实时服务器
  • Create empty database on live server (Posgres) 在实时服务器(Posgres)上创建空数据库
  • Update settings.py on the live server for all the relevant database etc settings 更新实时服务器上所有相关数据库等设置的settings.py
  • delete all migration files/folders and delete all *.pyc files 删除所有迁移文件/文件夹并删除所有* .pyc文件
  • python manage.py makemigrations python manage.py makemigrations

I get the error: django.db.utils.ProgrammingError: relation "myapp_mytable" does not exist . 我收到错误: 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. 我发现的唯一解决方案是进入我的settings.py文件并注释掉INSTALLED_APPS所有应用程序,并进入我的主urls.py文件并注释掉所有URL。

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 之后,我可以取消注释我的应用程序,并开始一个一个地迁移它们,即: python manage.py makemigrations appname然后python manage.py migrate python manage.py makemigrations appname

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. 当然,有一种方法可以告诉django,我已经创建了一个全新的空数据库,因此它需要首先进行初始迁移。 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. 我使用Ansible使服务器设置自动化,因此要求我执行所有此手动注释/取消注释操作和多次迁移是不好的。

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 . 因此,我在开发服务器上进行了以下尝试来尝试重新创建它们: 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. 我最终在@Daniel Roseman的评论中找到了问题。 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. 这是在创建一个模型实例(即:Correlation)并针对此实例调用get_entries()方法。 I then surrounded that in list(). 然后,我将其包围在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. 我删除了没有必要的list(),现在一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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