Python django模型未创建postgresql表
我在django项目中为我的reporter应用程序添加了一个简单的模型Python django模型未创建postgresql表,python,django,postgresql,Python,Django,Postgresql,我在django项目中为我的reporter应用程序添加了一个简单的模型 class Municipalities(models.Model): namelsad = models.CharField(max_length=100) geom = gismodels.MultiPolygonField(srid=4326) 当我python3 manage.py makemigrations reporter时 Operations to perform: Apply al
class Municipalities(models.Model):
namelsad = models.CharField(max_length=100)
geom = gismodels.MultiPolygonField(srid=4326)
当我python3 manage.py makemigrations reporter时
Operations to perform:
Apply all migrations: reporter
Running migrations:
No migrations to apply.
它表示在reporter中未检测到任何更改
Operations to perform:
Apply all migrations: reporter
Running migrations:
No migrations to apply.
然后当我python3 manage.py迁移报告器时
Operations to perform:
Apply all migrations: reporter
Running migrations:
No migrations to apply.
但是没有reporter_的postgresql数据库表
reporter包含在已安装的应用程序中
模型位于reporter应用程序的model.py文件中
我应该添加一个countries表,并在postgresql中手动删除它,然后尝试添加市政模型来创建一个表
市政当局也采用django_内容类型
但没有城市表
更新
将市政等级
更改为市政等级
python3 manage.py makemigrations reporter
Operations to perform:
Apply all migrations: reporter
Running migrations:
No migrations to apply.
然后它问我
Did you rename the reporter.Municipalities model to Muni? [y/N]
如果我点击y
然后运行迁移
给我
django.db.utils.ProgrammingError: table "reporter_municipalities" does not exist
但我很困惑,桌子根本不存在,也从来没有存在过
我现在根本无法迁移到DB,因为“reporter_music”当Django查找迁移时,它会搜索应用程序
reporter
,就像在您的安装的应用程序中配置的那样。它使用AppConfig
或应用程序名来检索完整的包名,即my_project.reporter
。此软件包必须在您的PYTHONPATH
中提供。它应该只在您的开发项目中可用,但可能是“安装”在您的virtualenv中。它可以在运行pip安装时发生。
(没有-e
)或(在某些配置中)运行测试(我在tox中看到过这种情况)
在这个场景中,python可以使用两个my_project.reporter
,但是您可以编辑/更新“第二个”。当您运行/manage.py makemigrations
时,python首先会找到您没有更改的代码(virtualenv中安装的代码),因此它不会找到更新
要检查您是否有此副本,您可以:
卸载项目(pip uninstall
),查看它是否引用了“符号链接”
如果是这样的话,你会看到
卸载:
将删除:
/data/PROGETTI/UNICEF/odk/.venv/lib/python3.6/site packages/.egg链接
是否继续?Y
注意该行末尾的egg链接
或
打开一个shell,键入导入为pkg;打印(打包文件)
并检查文件路径
我回答这个问题已经晚了,但是对于将来面临这个问题的人来说,在迁移文件夹的pycache文件夹中有扩展名为.pyc的文件。如果这些文件已经存在,只需删除它们,然后从头开始运行,即再次从makemigrations开始运行,希望您能得到它。通过删除除init之外的migrations文件夹中的文件,以及删除除init之外的pycache文件夹中的文件,我得到了同样的结果。很抱歉,我不能给出一个非常技术性的答案,但我通过pgAdmin直接创建了一个同名表,解决了这个问题。进行和运行迁移。此时,该表已被识别,但显然无法正常工作,因为它没有列。然后,我在models.py中注释掉了该模型,并进行了ran迁移,删除了该表。我在pgAdmin上检查表是否已在此时被删除。然后我删除了迁移文件(不知道这是否必要),在models.py中取消了对模型的注释,然后再次进行迁移并运行迁移。之后工作正常。您是否将应用程序包括在安装的应用程序中?是的,它在安装的应用程序中。您在哪个文件中声明了模式?@RedCricket model.py您是否介意从reporter
应用程序中删除您的迁移
目录,然后分别进行makemigrations
和migrate
吗?我也不确定这会实现什么。它不是任何已安装的pip应用程序,它是Django项目中的一个模型。好吧,我将不得不等待几天,直到我重新开始工作,你的解释有点让我不知所措。我要补充的是,我的项目不在虚拟环境中,我无法理解这一点——幸运的是,我还没有深入到我的项目中。最后我换了一个新的postgres DB