【发布时间】:2021-11-02 14:22:58
【问题描述】:
我用 django 创建了一个项目,它有 2 个应用程序。他们每个人都有一个models.py,我将在下面展示。当我尝试启动时出现问题:
python manage.py makemigratios
我收到以下错误:
Traceback (most recent call last):
File "/home/pablo/.local/share/virtualenvs/portfolio-cTVCjELO/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: no existe la relación «weatherapi_weatherstation»
LINE 1: ...gitude", "weatherapi_weatherstation"."token" FROM "weatherap...
...
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: no existe la relación «weatherapi_weatherstation»
LINE 1: ...gitude", "weatherapi_weatherstation"."token" FROM "weatherap...
^
上述异常是以下异常的直接原因:
它们是两个非常简单的模型,一切似乎都是正确的,但不可能启动 makemigrations。我也尝试过删除数据库,但它仍然是一样的。
Settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'portfolio',
'USER': 'pablo',
'PASSWORD': '1234',
'HOST': 'localhost',
'PORT': '5432',
}
}
模型 1:
from uuid import uuid1
from django.db import models
def generate_uid():
return uuid1().hex
# Create your models here.
class WeatherStation(models.Model):
uid = models.CharField(max_length=32, db_index=True, unique=True, default=generate_uid)
name = models.CharField(max_length=256, default="No name")
created_at = models.DateTimeField(auto_now_add=True)
latitude = models.FloatField()
longitude = models.FloatField()
token = models.CharField(max_length=40, unique=True,)
def __str__(self):
return self.name
class WeatherRecord(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
temperature = models.DecimalField(default=None, max_digits=4, decimal_places=2)
humidity = models.DecimalField(default=None, max_digits=4, decimal_places=2)
pressure = models.DecimalField(default=None, max_digits=6, decimal_places=2)
state = models.IntegerField(default=0)
weather_station = models.ForeignKey(WeatherStation, on_delete=models.CASCADE, default=None)
class Meta:
ordering = ['-created_at']
模型 2:
class Post(BaseModel):
title = models.CharField(max_length=200, verbose_name='Título')
slug = models.CharField(max_length=200, blank=True, null=True)
description = models.CharField(max_length=240, null=True, blank=True)
content = RichTextField(verbose_name='Contenido')
published_at = models.DateTimeField(default=now, verbose_name='Fecha de publicación')
image = models.ImageField(verbose_name='Imagen', upload_to='blog', null=True, blank=True)
author = models.ForeignKey(User, verbose_name='Autor', on_delete=models.CASCADE)
categories = models.ManyToManyField(Category, verbose_name='Categorias', related_name="get_posts")
url = models.URLField(blank=True, null=True)
【问题讨论】:
-
尝试在他们两个上使用
python manage.py makemigrations APPNAME,看看会发生什么 -
它也不起作用,但我发现了问题。它是 forms.py 中的一个查询。那里不应该有隐式查询,这会导致迁移失败。
标签: python django postgresql