在重写Django模型删除不必要的主键后,得到InvalidCursorName错误消息



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

一旦这些问题解决了,在进行迁移和成功迁移之后,当Django管理员试图向特定模型添加数据时,在点击添加按钮后,我得到了这个错误消息:

Traceback (most recent call last):
File "C:Usersfsoarurban_forest_boxvirtualenvlibsite-packagesdjangodbbackendsutils.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_projectuploadsimg_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应用同步。试着在另一个数据库上运行它,但不要忘记makemigration和migrate。

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

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

相关内容

  • 没有找到相关文章

最新更新