每当我尝试保存到 SQLite 数据库中的表时,都会出现以下错误:
外键不匹配 - “procedure_tbl”引用“filename_tbl”
在 models.py 中,这些是错误所指的表:
class FilenameTbl(models.Model):
rowid = models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='FileID', db_column='rowid')
filename = models.TextField(blank=True, null=True)
creation_datetime = models.TextField(blank=True, null=True)
class Meta:
managed = False
db_table = 'filename_tbl'
ordering = ['rowid']
class ProcedureTbl(models.Model):
rowid = models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ProcedureID', db_column='rowid')
...
filename_id = models.ForeignKey(FilenameTbl,db_column='filename_id', to_field='rowid',null=True,blank=True,on_delete=models.SET_NULL)
class Meta:
managed = False
db_table = 'procedure_tbl'
ordering = ['rowid']
可以从表和查询集中读取数据,如下所示返回正确的数据:
queryset = FilenameTbl.objects.values(
'rowid', 'filename',
'proceduretbl__rowid')
用于正确写入/更新 ProcedureTbl 表功能的原始 SQLite 命令。
如果我从 ProcedureTbl 中删除了 filename_id,则可以将数据保存到表中:
queryset = ProcedureTbl.objects.get(procedure_number=10)
queryset.reviewer_comments='can save this'
queryset.save()