简体   繁体   English

如何解决 Django 中的以下错误:“OperationalError:外键不匹配”

[英]How do I resolve the following error in Django: “OperationalError: foreign key mismatch”

I'm getting the following error whenever I attempt to save to the table in a SQLite database:每当我尝试保存到 SQLite 数据库中的表时,都会收到以下错误:

foreign key mismatch - "procedure_tbl" referencing "filename_tbl"外键不匹配 - “procedure_tbl”引用“filename_tbl”

In models.py, these are the tables that the error is referring to:在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']

Data can be read from the tables and querysets like the following return the correct data:可以从表和查询集中读取数据,如下所示返回正确的数据:

    queryset = FilenameTbl.objects.values(
        'rowid', 'filename',
        'proceduretbl__rowid')

Raw SQLite commands to write/update to the ProcedureTbl table function properly.用于正确写入/更新到 ProcedureTbl 表的原始 SQLite 命令。

If I removed filename_id from the ProcedureTbl, then data can be saved to the table:如果我从 ProcedureTbl 中删除了 filename_id,则数据可以保存到表中:

    queryset = ProcedureTbl.objects.get(procedure_number=10)
    queryset.reviewer_comments='can save this'
    queryset.save()

This is more of a workaround.这更像是一种解决方法。

In creation of the tables, I didn't create an alias for the ROWID and just referred to ROWID as the primary key.在创建表时,我没有为 ROWID 创建别名,只是将 ROWID 称为主键。 This worked fine for RAW SQLite commands but it seems Django doesn't handle ROWID very well.这适用于 RAW SQLite 命令,但似乎 Django 不能很好地处理 ROWID。

After creating aliases, the "foreign key mismatch" error went away:创建别名后,“外键不匹配”错误消失了:

class FilenameTbl(models.Model):
    filename = models.TextField(blank=True, null=True)
    creation_datetime = models.TextField(blank=True, null=True)
    filename_number = models.AutoField(primary_key=True, db_column='filename_number')

    class Meta:
        managed = False
        db_table = 'filename_tbl'
        ordering = ['filename_number']

# new ProcedureTbl

class ProcedureTbl(models.Model):
    ...
    filename_id = models.ForeignKey(FilenameTbl,db_column='filename_id',null=True,blank=True,on_delete=models.SET_NULL)
    procedure_number = models.AutoField(primary_key=True, db_column='procedure_number')

    class Meta:
        managed = False
        db_table = 'procedure_tbl'
        ordering = ['procedure_number']

I got the idea of creating aliases after reading these articles阅读这些文章后,我有了创建别名的想法

Why does referencing a SQLite rowid cause foreign key mismatch? 为什么引用 SQLite rowid 会导致外键不匹配?

Why can't you use SQLite ROWID as a Primary key? 为什么不能使用 SQLite ROWID 作为主键?

Thanks for everyone's time.谢谢大家的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 django.db.utils.OperationalError:外键不匹配-引用“ transcript_userprofile”的“ django_admin_log” - django.db.utils.OperationalError: foreign key mismatch - “django_admin_log” referencing “transcript_userprofile” Django SQLite外键不匹配错误 - Django SQLite foreign key mismatch error 外键不匹配错误 Django python - Foreign key mismatch error in Django python django.db.utils.OperationalError:外键不匹配 - “project_projectpage”引用“auth_user” - django.db.utils.OperationalError: foreign key mismatch - "project_projectpage" referencing "auth_user" django.db.utils.OperationalError:Shell 命令 forloop 中的外键不匹配 - django.db.utils.OperationalError: foreign key mismatch in Shell command forloop 在 Django model 中进行迁移时出现“外键不匹配”错误 - Getting 'foreign key mismatch' error when making migrations in Django model Django-将外键跟随到另一个外键 - Django - Following A Foreign Key To Another Foreign Key 如何解决 django 的权限错误? - How do I resolve a permission error with django? 您如何为模型及其遵循特定外键的所有子项创建Django表单? - How do you make a Django Form for a model and all of its children following a particular foreign key? 如何在Django中使用一对多/外键? - How do I get one to many/foreign key to work in django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM