InvalidCursorName:Django 管理错误引用了foreignkey中的错误列
我已经使用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 existThe 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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将“ _id”删除为后缀是修复。在这种情况下,
geoname_id
更改为geoname
修复了此问题。为什么?我不知道。 Django在幕后做的事情对我来说尚不清楚。这是更新的模型:Removing the '_id' as the suffix is the fix. In this case
geoname_id
changed togeoname
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: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.