InvalidCursorName:Django 管理错误引用了foreignkey中的错误列

发布于 2025-01-19 12:34:10 字数 2197 浏览 1 评论 0原文

我已经使用django的foreferkey对2个不受管理的表进行了建立关系:

class Product(BaseModel):
    publish_name = models.CharField(unique=True, max_length=40)
    # this works:
    associated_country = models.ForeignKey('geonames.Countryinfo', models.DO_NOTHING, db_column='published_country', blank=True, null=True)
    # this doesn't:
    associated_continent = models.ForeignKey('geonames.Continentcodes', on_delete=models.DO_NOTHING, db_column='published_continent' blank=True, null=True)

    class Meta:
        managed = True
        db_table = 'product'

class Continentcodes(models.Model):
    code = models.CharField(max_length=2, primary_key=True, unique=True)
    name = models.TextField(blank=True, null=True)
    geoname_id = models.ForeignKey('Geoname', models.DO_NOTHING, blank=True, null=True, unique=True)

    class Meta:
        managed = False
        db_table = 'geoname_continentcodes'

class Countryinfo(models.Model):
    iso_alpha2 = models.CharField(primary_key=True, max_length=2)
    country = models.TextField(blank=True, null=True)
    geoname = models.ForeignKey('Geoname', models.DO_NOTHING, blank=True, null=True)
    neighbours = models.TextField(blank=True, null=True)

    class Meta:
        ordering = ['country']
        managed = False
        db_table = 'geoname_countryinfo'
        verbose_name_plural = 'Countries'

当我去编辑“产品”的django admin页面中的条目时,我会看到:

无效的Cursorname at/admin/product/6/Change/Cursor “ _django_curs_140162796078848_sync_5”不存在

上述异常(列GeOname_contnoundCodes.geoname_id_id dik dik 不存在第1行:... ntcodes”。“代码”,“ geoname_contnoundcodes”。“名称”, “ geoname_c ... ^提示:也许您打算参考列 “ geoname_contnoundcodes.geoname_id”

看起来正试图引用 geoname_contnoundcodes.geoname_id_id_id 。我尝试在外国关系中添加to ='code',但似乎没有任何影响。

此外,如果我评论implocy_continine字段,则ippers_country关系似乎可以正常工作。 Assocy_continine是给出一些问题的列。

这是有关该表中表的外观的更多背景:

I've setup a relationship using django's ForeignKey against 2 unmanaged tables like so:

class Product(BaseModel):
    publish_name = models.CharField(unique=True, max_length=40)
    # this works:
    associated_country = models.ForeignKey('geonames.Countryinfo', models.DO_NOTHING, db_column='published_country', blank=True, null=True)
    # this doesn't:
    associated_continent = models.ForeignKey('geonames.Continentcodes', on_delete=models.DO_NOTHING, db_column='published_continent' blank=True, null=True)

    class Meta:
        managed = True
        db_table = 'product'

class Continentcodes(models.Model):
    code = models.CharField(max_length=2, primary_key=True, unique=True)
    name = models.TextField(blank=True, null=True)
    geoname_id = models.ForeignKey('Geoname', models.DO_NOTHING, blank=True, null=True, unique=True)

    class Meta:
        managed = False
        db_table = 'geoname_continentcodes'

class Countryinfo(models.Model):
    iso_alpha2 = models.CharField(primary_key=True, max_length=2)
    country = models.TextField(blank=True, null=True)
    geoname = models.ForeignKey('Geoname', models.DO_NOTHING, blank=True, null=True)
    neighbours = models.TextField(blank=True, null=True)

    class Meta:
        ordering = ['country']
        managed = False
        db_table = 'geoname_countryinfo'
        verbose_name_plural = 'Countries'

When I go to edit an entry in the django admin page for 'Products' I see this:

InvalidCursorName at /admin/product/6/change/ cursor
"_django_curs_140162796078848_sync_5" does not exist

The above exception (column geoname_continentcodes.geoname_id_id does
not exist LINE 1: ...ntcodes"."code", "geoname_continentcodes"."name",
"geoname_c...
^ HINT: Perhaps you meant to reference the column
"geoname_continentcodes.geoname_id"

It looks like it's trying to reference geoname_continentcodes.geoname_id_id for some reason. I have tried adding to='code' in the ForeignKey relationship, but it doesn't seem to effect anything.

Additionally, the associated_country relationship seems to work just fine if I comment out the associated_continent field. The associated_continent is the column that is giving some problem.

Here is some more context about what the table looks like in the database:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黎夕旧梦 2025-01-26 12:34:10

将“ _id”删除为后缀是修复。在这种情况下,geoname_id更改为geoname修复了此问题。为什么?我不知道。 Django在幕后做的事情对我来说尚不清楚。这是更新的模型:

class Continentcodes(models.Model):
    code = models.CharField(max_length=2, primary_key=True, unique=True)
    name = models.TextField(blank=True, null=True)
    # remove '_id' as the suffix here
    geoname = models.ForeignKey('Geoname', models.DO_NOTHING, blank=True, null=True, unique=True)

    class Meta:
        managed = False
        db_table = 'geoname_continentcodes'

Removing the '_id' as the suffix is the fix. In this case geoname_id changed to geoname fixes this. Why? I have no idea. Django is doing something behind the scenes that is not clear to me. Here is the updated model:

class Continentcodes(models.Model):
    code = models.CharField(max_length=2, primary_key=True, unique=True)
    name = models.TextField(blank=True, null=True)
    # remove '_id' as the suffix here
    geoname = models.ForeignKey('Geoname', models.DO_NOTHING, blank=True, null=True, unique=True)

    class Meta:
        managed = False
        db_table = 'geoname_continentcodes'
空‖城人不在 2025-01-26 12:34:10

Django将_id添加到末端以区分表中的对象和ID列,GeOname_ID将返回表ID,而GeOname将返回对象。

Django adds _id to the end to differentiate between the object and the id column in the table, geoname_id will return the table id where as geoname will return the object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文