OperationalError: foreign key mismatch

878 views
Skip to first unread message

Wai Yeung

unread,
Jul 26, 2021, 4:53:15 PM7/26/21
to Django users
Hello,

I'm getting the following error whenever I attempt to save to the table in a SQLite database:

foreign key mismatch - "procedure_tbl" referencing "filename_tbl"

In models.py, these are the tables that the error is refering to:

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.

If I removed filename_id from the ProcedureTbl, then data can be saved to the table.

Any insight into the issue would be much appreciated.

Cheers,
Wai



DJANGO DEVELOPER

unread,
Jul 27, 2021, 12:32:27 AM7/27/21
to django...@googlegroups.com
can you please  change your object from this:
filename_id = models.ForeignKey(FilenameTbl,db_column='filename_id', to_field='rowid',null=True,blank=True,on_delete=models.SET_NULL)
to :
filename_id = models.ForeignKey(FilenameTbl,db_column='filename', to_field='rowid',null=True,blank=True,on_delete=models.SET_NULL)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b90d237f-8c29-42ef-897a-34221704699dn%40googlegroups.com.

Wai Yeung

unread,
Jul 27, 2021, 12:10:51 PM7/27/21
to django...@googlegroups.com
Hello,

Thanks for the suggestion.  I actually changed it to the following to as to not have the "_id" suffix:

filename_number = models.ForeignKey(FilenameTbl,db_column='filename_number', to_field='rowid',null=True,blank=True,on_delete=models.SET_NULL)

This is what eventually worked for me:

Cheers,
Wai

You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/nqYRTN3m6Bg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAKPY9pkH2pp3bkARsviqUdmn007Hc30ckrVOPH4yTBakJqXjDw%40mail.gmail.com.

DJANGO DEVELOPER

unread,
Jul 28, 2021, 12:16:02 AM7/28/21
to Django users

Wai Yeung

unread,
Jul 28, 2021, 1:32:33 PM7/28/21
to django...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages