Django 查询因未使用、未创建或未引用的 _id 而失败
Django query fails with _id that is not used or created or referenced to
虽然这是我第一次尝试 JOIN tables,但我以前使用过查询集,但目前还没有用。
我正在使用 django 3.2 和 python 3.8.1
我的models.py
class Mainjoinbook(models.Model):
fullsitename = models.TextField(primary_key=True)
creationdate = models.DateTimeField()
entrytypeid = models.BigIntegerField(blank=True, null=True)
title = models.TextField(blank=True, null=True)
tickettype = models.TextField(blank=True, null=True)
ticket = models.TextField(blank=True, null=True)
status = models.TextField(blank=True, null=True)
class Meta:
managed = False
db_table = 'mainlogbook'
class Sitelocation(models.Model):
site_name = models.TextField(primary_key=True)
latitude = models.TextField(blank=True, null=True)
longitude = models.TextField(blank=True, null=True)
sites = models.ForeignKey(Mainjoinbook, on_delete=models.DO_NOTHING)
class Meta:
managed = False
db_table = 'tblsiteaccess'
我正在尝试从加入我的 views.py
的两个 table 中获取所有值
qrylocations = Sitelocation.objects.select_related('sites').filter(sites__status='OPEN')
这会导致此错误,因为该列是由 django 创建但不属于 table。我仍然无法解决这个问题,因为我尝试了很多选择,但总是会遇到某种错误,我希望有人能帮助我看看我在加入 tables 时做错了什么主键已定义
psycopg2.errors.UndefinedColumn: column tblsiteaccess.sites_id does not exist
显示的 SQL 输出如下。
来自 qrylocations.query
的输出
SELECT "tblsiteaccess"."site_name", "tblsiteaccess"."latitude", "tblsiteaccess"."longitude", "tblsiteaccess"."sites_id", "mainlogbook"."fullsitename", "mainlogbook"."log_id", "mainlogbook"."creationdate", "mainlogbook"."entrytypeid", "mainlogbook"."title", "mainlogbook"."tickettype", "mainlogbook"."ticket", "mainlogbook"."status" FROM "tblsiteaccess" INNER JOIN "mainlogbook" ON ("tblsiteaccess"."sites_id" = "mainlogbook"."fullsitename") WHERE "mainlogbook"."status" = OPEN
一个ForeignKey
自然需要数据库中的一列table。由于 site_name
本身是主键,因此您应该在此处将其用作 ForeignKey
,实际上,而不是 ForeignKey
这需要是 OneToOneField
[Django docs],因为它也是一个主键并且必须是唯一的:
class Sitelocation(models.Model):
site_name = models.OneToOneField(
Mainjoinbook,
on_delete=models.CASCADE,
primary_key=True,
db_column='site_name'
)
latitude = models.TextField(blank=True, null=True)
longitude = models.TextField(blank=True, null=True)
class Meta:
managed = False
db_table = 'tblsiteaccess'
虽然这是我第一次尝试 JOIN tables,但我以前使用过查询集,但目前还没有用。 我正在使用 django 3.2 和 python 3.8.1
我的models.py
class Mainjoinbook(models.Model):
fullsitename = models.TextField(primary_key=True)
creationdate = models.DateTimeField()
entrytypeid = models.BigIntegerField(blank=True, null=True)
title = models.TextField(blank=True, null=True)
tickettype = models.TextField(blank=True, null=True)
ticket = models.TextField(blank=True, null=True)
status = models.TextField(blank=True, null=True)
class Meta:
managed = False
db_table = 'mainlogbook'
class Sitelocation(models.Model):
site_name = models.TextField(primary_key=True)
latitude = models.TextField(blank=True, null=True)
longitude = models.TextField(blank=True, null=True)
sites = models.ForeignKey(Mainjoinbook, on_delete=models.DO_NOTHING)
class Meta:
managed = False
db_table = 'tblsiteaccess'
我正在尝试从加入我的 views.py
的两个 table 中获取所有值qrylocations = Sitelocation.objects.select_related('sites').filter(sites__status='OPEN')
这会导致此错误,因为该列是由 django 创建但不属于 table。我仍然无法解决这个问题,因为我尝试了很多选择,但总是会遇到某种错误,我希望有人能帮助我看看我在加入 tables 时做错了什么主键已定义
psycopg2.errors.UndefinedColumn: column tblsiteaccess.sites_id does not exist
显示的 SQL 输出如下。
来自 qrylocations.query
的输出SELECT "tblsiteaccess"."site_name", "tblsiteaccess"."latitude", "tblsiteaccess"."longitude", "tblsiteaccess"."sites_id", "mainlogbook"."fullsitename", "mainlogbook"."log_id", "mainlogbook"."creationdate", "mainlogbook"."entrytypeid", "mainlogbook"."title", "mainlogbook"."tickettype", "mainlogbook"."ticket", "mainlogbook"."status" FROM "tblsiteaccess" INNER JOIN "mainlogbook" ON ("tblsiteaccess"."sites_id" = "mainlogbook"."fullsitename") WHERE "mainlogbook"."status" = OPEN
一个ForeignKey
自然需要数据库中的一列table。由于 site_name
本身是主键,因此您应该在此处将其用作 ForeignKey
,实际上,而不是 ForeignKey
这需要是 OneToOneField
[Django docs],因为它也是一个主键并且必须是唯一的:
class Sitelocation(models.Model):
site_name = models.OneToOneField(
Mainjoinbook,
on_delete=models.CASCADE,
primary_key=True,
db_column='site_name'
)
latitude = models.TextField(blank=True, null=True)
longitude = models.TextField(blank=True, null=True)
class Meta:
managed = False
db_table = 'tblsiteaccess'