Reputation: 170
After learning most of my primary keys did not have to be hardcoded in Django models, I decided to remove them all. In order to have Django-admin up and running I had to solve a few issues first, which I did by deleting all migrations files.
Once those issues were out of the way, after making migrations and migrating sucessfully again, while in Django admin trying to add data to a particular model, right after clicking the add button I got this error message:
Traceback (most recent call last):
File "C:\Users\fsoar\urban_forest_box\virtualenv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedColumn: column app_species_catalog_nome_popular.id does not exist
LINE 1: ...188_sync_1" NO SCROLL CURSOR WITH HOLD FOR SELECT "app_speci...
This lead to a series of exceptions which ledd to another series of exceptions of which the last erros message was:
psycopg2.errors.InvalidCursorName: cursor "_django_curs_17188_sync_1" does not exist
models.py is a bit long, so I am pasting only the main model of this app, which is the one I was using to add data when it happened, and which speaks for itself regarding my Python/Django skills level :)
I would like to understand what is going on, and I imagine it will take several months before I get to that, but most important, I would like to fix it first, so as to keep my learning journey going.
# ARVORE
class Img_Arvore(models.Model):
img_arvore = models.ImageField(upload_to=r'urban_forest_django_project\uploads\img_arvores_completas')
class Meta:
verbose_name = "Foto da árvore"
verbose_name_plural = "Fotos da árvore"
class Arvore(models.Model):
nome_cientifico = models.CharField("Nome científico", max_length=200, help_text="Nome científico completo", primary_key=True)
nomes_populares = models.ManyToManyField(Nome_Popular, verbose_name="Nomes populares")
estados_de_conservacaos = (
('EX', 'Extinta'),
('EW', 'Extinta no ambiente silvestre'),
('CR', 'Criticamente em perigo'),
('EN', 'Em perigo'),
('VU', 'Vulnerável'),
('NT', 'Quase ameaçada'),
('LC', 'Menos preocupante'),
('DD', 'Dados insuficientes'),
('NE', 'Não avaliado')
)
estado_de_conservacao = models.CharField("Estado de conservação", max_length=50, choices=estados_de_conservacaos)
botanic_description = models.TextField('Descrição botânica', blank=True)
create_date = models.DateTimeField(auto_now_add=True)
update_date = models.DateTimeField(blank=True, null=True)
taxonomia = models.OneToOneField(Taxonomia, on_delete=SET_NULL, null=True, blank=True)
biotipo = models.ForeignKey(Biotipo, on_delete=SET_NULL, null=True, blank=True)
dendrometria = models.ForeignKey(Dendrometria, on_delete=SET_NULL, null=True, blank=True)
peculiaridades = models.ForeignKey(Peculiaridade, on_delete=SET_NULL, null=True, blank=True)
caule = models.ForeignKey(Caule, on_delete=SET_NULL, null=True, blank=True)
raiz = models.ForeignKey(Raiz, on_delete=SET_NULL, null=True, blank=True)
folha = models.ForeignKey(Folha, on_delete=SET_NULL, null=True, blank=True)
flor = models.ForeignKey(Flor, on_delete=SET_NULL, null=True, blank=True)
fruto = models.ForeignKey(Fruto, on_delete=SET_NULL, null=True, blank=True)
distribuicao_estadual = models.ManyToManyField(UF_Brasileira, blank=True)
distribuicao_regional = models.ManyToManyField(Regiao_Brasileira, blank=True)
dominio_fitogeografico = models.ManyToManyField(Bioma_Brasileiro, blank=True)
vegetacao_encontrada = models.ManyToManyField(Vegetacao_Brasileira, blank=True)
maiores_informacoes = models.ForeignKey(Link_Externo, on_delete=SET_NULL, null=True, blank=True)
class Meta:
verbose_name = "Árvore"
verbose_name_plural = "Árvores"
def __str__(self):
return self.nome_cientifico
Upvotes: 0
Views: 459
Reputation: 117
Did you also drop (delete) your database? Your database changes with migrations, So if you delete migrations and you dont drop your database, database will be confused because it is not synced with your django app. Try to run it on another database, but dont forget to makemigration and also migrate.
You should think of migrations as a version control system for your database schema. makemigrations is responsible for packaging up your model changes into individual migration files - analogous to commits - and migrate is responsible for applying those to your database.
For more information : https://docs.djangoproject.com/en/3.2/topics/migrations/
Upvotes: 1