Search code examples
sqlsqliteforeign-keysdatagrip

Activate foreign key constraints error in dataGrip


I'm working with DataGrip and sqlite. I have a table suppliers and another named products :

CREATE TABLE Suppliers (id INTEGER NOT NULL PRIMARY KEY,
nom TEXT,
siege_social TEXT);

CREATE TABLE Products (id INTEGER NOT NULL PRIMARY KEY,
nom TEXT,
prix REAL,
supplier INTEGER,
FOREIGN KEY (supplier) REFERENCES Suppliers(id)
);

I tried PRAGMA foreign_keys = ON; to activate the foreign keys constraint as my teacher do. However when I try to insert a product while having the supplier table empty, I get no error :

INSERT INTO Products values (1, "Lunettes Cobra", 30, 1);

I expected this : FOREIGN KEY constraint failed

Do you know how I could make it raise an error for this ?

Thanks !


Solution

  • Executing pragma statement enables that setting only for currents session. Console and editor perform in different sessions.

    Just go to advanced tab of the data source settings and set it there. It'll be applied for all sessions.

    enter image description here