Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite - Foreign Keys Contraints - IOs 5

Tags:

sqlite

ios

key

it seems that Foreign Keys Constraints are supported since version 3.6.x in SQLite. The version of SQLite on IOS5.0 is 3.7.7 (found in sqlite3.h).

But when I try to insert a row in a table that has a constraint, my row is correctly inserted even if the related foreign key is not existing. I have no error.

Doing the same insert statement using apps like Navicat gives me a "Constraint violation error"

Do you know if foreign keys are supported on IOs 5 ?

Here is the Database Schema:

CREATE TABLE artist(
  artistid    INTEGER PRIMARY KEY, 
  artistname  TEXT
)

CREATE TABLE "track" (
     "trackid" INTEGER PRIMARY KEY AUTOINCREMENT,
     "trackname" TEXT,
     "trackartist" INTEGER,
    CONSTRAINT "trackartist" FOREIGN KEY ("trackartist") REFERENCES "artist" ("artistid") ON DELETE CASCADE ON UPDATE CASCADE)

Really simple, isn't it ?

Thanks Emmanuel

like image 848
ecaste Avatar asked Jan 05 '12 16:01

ecaste


People also ask

Does SQLite support foreign keys?

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. To check whether your current version of SQLite supports foreign key constraints or not, you use the following command.

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.

Which constraints is applied on foreign key?

The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables. A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in another table.


1 Answers

Foreign keys are disabled by default. You have to enable them separately for each connection. The setting isn't "sticky". You have to do this every time you connect to a SQLite database.

PRAGMA foreign_keys = ON;

Odds are good that Navicat takes care of that for you. In your own code, it's your job.

like image 133
Mike Sherrill 'Cat Recall' Avatar answered Sep 29 '22 21:09

Mike Sherrill 'Cat Recall'