Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knowing specifically which FOREIGN KEY constraint failed in SQLite

Tags:

sqlite

I get the SQLite error message "FOREIGN KEY constraint failed". That's the complete error information (besides a part of the SQL query) and it's not helpful. (In fact it's just as good (or bad) as Oracle error messages.) I need to know the name of the constraint to investigate the issue in my program. Unfortunately there's no web support platform to discuss this with an SQLite community. Does somebody know how to get more information about the error out of that SQLite library?

I'm specifically using the System.Data.SQLite library for .NET but the error message comes directly from the core and there are no additional exception properties that could help me.

like image 256
ygoe Avatar asked Mar 30 '16 07:03

ygoe


People also ask

How do you identify foreign key constraints?

Open the Table Designer for the table containing the foreign key you want to view, right-click in the Table Designer, and choose Relationships from the shortcut menu. In the Foreign Key Relationships dialog box, select the relationship with properties you want to view.

How do I fix foreign key constraint failure?

The error message itself showing there is a foreign key constraint error, which means you are deleting a parent table where the child table contains the Primary table identifier as a foreign key. To avoid this error, you need to delete child table records first and after that the parent table record.

Does SQLite enforce foreign key constraints?

SQLite has supported foreign key constraint since version 3.6. 19. The SQLite library must also be compiled with neither SQLITE_OMIT_FOREIGN_KEY nor SQLITE_OMIT_TRIGGER.

How do you ignore foreign key constraints?

To disable a foreign key constraint for INSERT and UPDATE statements. In Object Explorer, expand the table with the constraint and then expand the Keys folder. Right-click the constraint and select Modify. In the grid under Table Designer, select Enforce Foreign Key Constraint and select No from the drop-down menu.


1 Answers

Due to the way in which deferred FK constraints are implemented in SQLite, this information is not available when the error is raised.

You could reimplement the FK checks as triggers. Alternatively, log the values in the failed command, and look up the data by hand.

like image 132
CL. Avatar answered Oct 26 '22 09:10

CL.