/ admin列+ _id上的Django 2.1 ProgrammingError不存在

时间:2019-02-20 14:49:18

标签: django python-3.x postgresql django-models

在尝试运行第二个 Django 2.1 / Postgres 10 项目时,遇到以下编程错误:

ProgrammingError at /admin/vocs_app/subsubscriber/

column sub_subscriber.sub_prev_sst_uid_id does not exist
LINE 1: ...", "sub_subscriber"."sub_next_recharge_datetime", "sub_subsc...
                                                             ^
HINT:  Perhaps you meant to reference the column "sub_subscriber.sub_prev_sst_uid".

我可以打开应用程序的admin app,即127.0.0.1:8000/admin/vocs_app。 它列出了我数据库中所有导入的模型;为了说明我的情况,我相信以下课程是足够的:

(摘自my_site/vocs_app/models.py:)

from django.db import models

class VneVirtualNetwork(models.Model):
    vne_uid = models.BigAutoField(primary_key=True)
    vne_nop_uid = models.ForeignKey(NopNetworkOperator, models.DO_NOTHING, db_column='vne_nop_uid')
    vne_name = models.CharField(max_length=50)
    vne_code = models.CharField(max_length=50)
    vne_external_id = models.CharField(max_length=100, blank=True, null=True)
    vne_idd_code = models.CharField(max_length=5)
    vne_sn_length = models.IntegerField()
    createdon = models.DateTimeField()
    createdby = models.CharField(max_length=150)
    createdfrom = models.CharField(max_length=150)
    modifiedon = models.DateTimeField()
    modifiedby = models.CharField(max_length=150)
    modifiedfrom = models.CharField(max_length=150)

    class Meta:
        managed = False
        db_table = 'vne_virtual_network'

class SstSystemStatus(models.Model):
    sst_uid = models.BigAutoField(primary_key=True)
    sst_name = models.CharField(max_length=50)
    sst_description = models.CharField(max_length=100, blank=True, null=True)
    startdate = models.DateField(blank=True, null=True)
    enddate = models.DateField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'sst_system_status'

class SubSubscriber(models.Model):
    sub_uid = models.BigAutoField(primary_key=True)
    sub_vne_uid = models.ForeignKey('VneVirtualNetwork', models.DO_NOTHING, db_column='sub_vne_uid')
    sub_rpl_uid = models.ForeignKey(RplRatePlan, models.DO_NOTHING, db_column='sub_rpl_uid')
    sub_account_user_id = models.CharField(max_length=100, blank=True, null=True)
    sub_external_id = models.CharField(max_length=100)
    sub_hzn_uid = models.ForeignKey(HznHomezoneName, models.DO_NOTHING, db_column='sub_hzn_uid')
    sub_low_balance_trigger = models.BooleanField()
    sub_first_call_datetime = models.DateTimeField(blank=True, null=True)
    sub_last_enabled_datetime = models.DateTimeField(blank=True, null=True)
    sub_last_disabled_datetime = models.DateTimeField(blank=True, null=True)
    sub_last_recharge_datetime = models.DateTimeField(blank=True, null=True)
    sub_next_recharge_datetime = models.DateTimeField(blank=True, null=True)
    sub_prev_sst_uid = models.ForeignKey(SstSystemStatus, related_name='sub_prev_sst_uid',on_delete=models.DO_NOTHING)
    sub_sst_uid = models.ForeignKey(SstSystemStatus, related_name='sub_sst_uid',on_delete=models.DO_NOTHING)
    startdatetime = models.DateTimeField()
    enddatetime = models.DateTimeField()
    createdon = models.DateTimeField()
    createdby = models.CharField(max_length=150)
    createdfrom = models.CharField(max_length=150)
    modifiedon = models.DateTimeField()
    modifiedby = models.CharField(max_length=150)
    modifiedfrom = models.CharField(max_length=150)

    class Meta:
        managed = False
        db_table = 'sub_subscriber'

class SubSubscriber两次引用表/类 sst_uid外键 SstSystemStatus(以前和当前状态)。似乎Django不喜欢它。诸如VneVirtualNetwork之类的其他表(包含一个“单个”外键引用)不会引起任何问题。管理GUI允许我显示和更改其数据。

该错误消息表明Django试图用sub_prev_sst_uid来补充字段'_id'的名称。如果我注释文件 model.py 中的相关行并尝试显示订户表,这将导致相同的错误,这一次是使用字段sub_sst_uid。如何防止Django附加'_id'

预先感谢您的任何建议。

1 个答案:

答案 0 :(得分:1)

默认情况下,Django将使用相关名称和/或所选的类字段名称,添加后缀“ _id”,然后尝试在数据库中找到该名称的列。除非您明确定义db_column如下:

sub_sst_uid = models.ForeignKey('SstSystemStatus', db_column='sub_sst_uid', related_name='sub_sst_uid', on_delete=models.DO_NOTHING)

sub_prev_sst_uid = models.ForeignKey('SstSystemStatus', db_column='sub_prev_sst_uid', related_name='sub_prev_sst_uid', on_delete=models.DO_NOTHING)