简体   繁体   English

django model 未创建 postgresql 表

[英]django model not creating postgresql table

I have added a simple model to my reporter app in my django project我在 django 项目中的记者应用程序中添加了一个简单的 model

class Municipalities(models.Model):
    namelsad = models.CharField(max_length=100)
    geom = gismodels.MultiPolygonField(srid=4326)

when I python3 manage.py makemigrations reporter当我python3 manage.py makemigrations reporter

it says no changes detected in reporter它说no changes detected in reporter

then when I python3 manage.py migrate reporter然后当我python3 manage.py migrate reporter

Operations to perform:
  Apply all migrations: reporter
Running migrations:
  No migrations to apply.

but there is no postgresql database table of reporter_municipalities但是没有postgresql数据库表reporter_municipalities

reporter is included in installed apps记者包含在已安装的应用程序中

Municipalities model is in the model.py file in reporter app自治市 model 位于记者应用程序的 model.py 文件中

I should add I had a counties table and manually deleted it in postgresql and tried adding the municipalities model to create a table我应该添加我有一个县表并在 postgresql 中手动删除它并尝试添加自治市 model 以创建一个表

municipalities is also in the django_content_type市政当局也在 django_content_type

在此处输入图像描述

but there is no table of municipalities但没有市镇表

update更新

Changed Class Municipalities to Class MuniClass Municipalities更改为Class Muni

python3 manage.py makemigrations reporter

then it asked me然后它问我

Did you rename the reporter.Municipalities model to Muni? [y/N] 

if I click y如果我点击 y

then run migrate然后运行迁移

gives me给我

django.db.utils.ProgrammingError: table "reporter_municipalities" does not exist

but I am so confused that table does not and never existed!!但我很困惑,桌子不存在,也不存在!

I cannot migrate at all to the DB now because of "reporter_municipalities"由于“reporter_municipalities”,我现在根本无法迁移到数据库

When Django looks for migrations, it searches for the app reporter as is it configured in your INSTALLED_APPS .当 Django 查找迁移时,它会按照您在INSTALLED_APPS配置搜索应用reporter程序。 It uses AppConfig or application name to retrieve the full package name, ie my_project.reporter .它使用AppConfig或应用程序名称来检索完整的包名称,即my_project.reporter This package must be available in your PYTHONPATH .这个包必须在你的PYTHONPATH可用。 It should be available only in you development project, but could happen that it is "installed" in your virtualenv.它应该只在您的开发项目中可用,但它可能会“安装”在您的 virtualenv 中。 It can happen running pip install .运行pip install .可能会发生这种情况pip install . (without -e ) or (with some configurations) running tests (I have seen this happen with tox). (没有-e )或(有一些配置)运行测试(我已经看到这种情况发生在 tox 上)。

In this scenario you have two my_project.reporter available to python, but you edit/update the "second" one.在这种情况下,您有两个my_project.reporter可用于 python,但您编辑/更新“第二个”。 When you run ./manage.py makemigrations , python first find the code that you did not change (the installed one in your virtualenv), so it does not find the updates.当您运行./manage.py makemigrations ,python 首先找到您没有更改的代码(virtualenv 中已安装的代码),因此它找不到更新。

To check if you have this duplication, you can:要检查您是否有此重复,您可以:

  1. uninstall your project ( pip uninstall <django-project> ) to see if it refers to a "symbolic link"卸载您的项目( pip uninstall <django-project> )以查看它是否指代“符号链接”

    If this is the case you will see something like Uninstalling <PROJECT>: Would remove: /data/PROGETTI/UNICEF/odk/.venv/lib/python3.6/site-packages/<PROJECT>.egg-link Proceed (y/n)? y如果是这种情况,您将看到类似Uninstalling <PROJECT>: Would remove: /data/PROGETTI/UNICEF/odk/.venv/lib/python3.6/site-packages/<PROJECT>.egg-link Proceed (y/n)? y Uninstalling <PROJECT>: Would remove: /data/PROGETTI/UNICEF/odk/.venv/lib/python3.6/site-packages/<PROJECT>.egg-link Proceed (y/n)? y

    Note the egg-link at the end of the line.请注意该行末尾的egg-link

OR或者

  1. open a shell and type import <full_path_of_reporter> AS pkg; print(pkg.__file__)打开一个 shell 并输入import <full_path_of_reporter> AS pkg; print(pkg.__file__) import <full_path_of_reporter> AS pkg; print(pkg.__file__) and check the filepath import <full_path_of_reporter> AS pkg; print(pkg.__file__)并检查文件路径

Sorry I can't give a very technical answer but I got round this my creating a table with the same name directly via pgAdmin.对不起,我不能给出一个非常技术性的答案,但我解决了这个问题,我直接通过 pgAdmin 创建了一个同名的表。 Making and running migrations.进行和运行迁移。 At this point the table was recognised but obviously didn't work correctly because it had no columns.此时表已被识别,但显然无法正常工作,因为它没有列。 I then commented out the model in models.py and made ran migrations which removed the table.然后我在 models.py 中注释掉模型并进行了删除表的运行迁移。 I checked on pgAdmin that the table had been removed by this point.我在 pgAdmin 上检查了该表已被删除。 I then deleted the migrations files (no idea if this was necessary), uncommented the model in models.py and then made and ran migrations again.然后我删除了迁移文件(不知道是否有必要),在 models.py 中取消注释模型,然后再次进行迁移。 Worked fine after that.在那之后工作得很好。

I'm late to the question but for people facing this in future, there are files inside the pycache folder inside the migrations folder with .pyc extension.我对这个问题迟到了,但对于将来面临这个问题的人来说,迁移文件夹内的pycache文件夹中存在扩展名为 .pyc 的文件。 Just delete those files if they are already there and run from starting, ie, start from makemigrations again and hopefully you'll get it.只要删除这些文件,如果它们已经存在并从头开始运行,即再次从 makemigrations 开始,希望你能得到它。 I got it the same way by deleting the files in migrations folder other than init and deleting the files inside pycache folder other than init .我通过删除文件夹迁移以外init和删除里面的文件夹pycache以外的init得到了同样的方式。

ty running python./manage.py makemigrations "app_name" as the last argument, then the migrate command. ty 运行 python./manage.py makemigrations "app_name" 作为最后一个参数,然后是 migrate 命令。 this worked for me when having the same issue.当遇到同样的问题时,这对我有用。

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

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