Issue
I have created a project with django which has 2 apps. Each of them has a models.py that I will show below. The problem occurs when I try to launch:
python manage.py makemigratios
I get the following error:
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...
^
The above exception was the direct cause of the following exception:
They are two very simple models and everything seems to be correct but it is impossible to launch makemigrations. I have also tried to drop the DB but it is still the same.
Settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'portfolio',
'USER': 'pablo',
'PASSWORD': '1234',
'HOST': 'localhost',
'PORT': '5432',
}
}
Models 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']
Models 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)
Solution
Solved: The problem was in the forms.py file there was an implicit query which should not be put there. Because of this it was not possible to do the migrations. When I removed that query everything worked again.
Answered By - Pablo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.