'InvalidCursorName : Django Admin error referencing wrong column in ForeignKey

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



Solution 1:[1]

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'

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 its30
Solution 2 lonny