1
votes

I have defined a foreign key. To check it, i insert wrong values to the table that has the foreign key. No error were printed and the values were added succefully. I do not know if i am running some old version of sqlite3 or something like that, i am completely new to this area.

create table ref(value1 int ,value2,primary key(value1));

create table for(value1 int,value3 int,primary key(value3),foreign key(value1)references ref(value1));

insert into for values (1,1);

This was added succefully.

1
Have you enabled Foreign Keys (PRAGMA foreign_keys = ON;)? as per 2. Enabling Foreign Key Support - MikeT

1 Answers

3
votes

Foreign key constraints are disabled by default as it is explained here: SQLite Foreign Key Support.
To enable them execute first:

PRAGMA foreign_keys = ON;

See the demo.