重写 Django 模型以删除不必要的主键后,出现 InvalidCursorName 错误消息

问题描述 投票:0回答:1

在了解到我的大部分主键不必在 Django 模型中进行硬编码后,我决定将它们全部删除。为了让 Django-admin 启动并运行,我必须首先解决一些问题,我通过删除所有迁移文件来做到这一点。

一旦这些问题解决了,在进行迁移并再次成功迁移后,在 Django 管理中尝试将数据添加到特定模型时,单击添加按钮后,我收到此错误消息:

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...

这导致了一系列异常,进而导致了另一系列异常,其中最后一个错误消息是:

psycopg2.errors.InvalidCursorName: cursor "_django_curs_17188_sync_1" does not exist

models.py 有点长,所以我只粘贴这个应用程序的主模型,这是我在发生数据时用来添加数据的模型,这本身就说明了我的 Python/Django 技能水平:)

我想了解发生了什么,我想我需要几个月的时间才能做到这一点,但最重要的是,我想先解决它,以便继续我的学习之旅。

# 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

django django-models django-admin psycopg2
1个回答
1
投票

您是否也删除了您的数据库? 你的数据库会随着迁移而改变,所以如果你删除迁移并且不删除你的数据库,数据库将会很混乱,因为它没有与你的 django 应用程序同步。尝试在另一个数据库上运行它,但不要忘记进行迁移并进行迁移。

您应该将迁移视为数据库模式的版本控制系统。 makemigrations 负责将模型更改打包到单独的迁移文件中(类似于提交),而 migrate 负责将这些更改应用到您的数据库。

了解更多信息:https://docs.djangoproject.com/en/3.2/topics/migrations/

© www.soinside.com 2019 - 2024. All rights reserved.