Home:ALL Converter>Is it allowed to have null foreign key in sqlite when PRAGMA foreign_keys=ON?

Is it allowed to have null foreign key in sqlite when PRAGMA foreign_keys=ON?

Ask Time:2013-05-01T00:42:47         Author:Rupesh

Json Formatter

according to this null foreign keys are allowed unless and until we are adding the appropriate "NOT NULL" constraint to the schema.

but I am seeing the some different behavior,

sqlite> PRAGMA Foreign_keys;
1
sqlite> create table proc (
sqlite>   pid integer,
sqlite>   name text,
sqlite>   ppid integer,
sqlite>   foreign key (ppid) references proc (id)
sqlite> );
sqlite> .schema proc
CREATE TABLE proc (
  pid integer,
  name text,
  ppid integer,
  foreign key (ppid) references proc (id)
);

sqlite> insert into proc (pid, name, ppid)
sqlite> values (0, "init", null);
Error: foreign key mismatch

sqlite> PRAGMA Foreign_keys=OFF;
sqlite> PRAGMA Foreign_keys;
0

sqlite> insert into proc (pid, name, ppid)
sqlite> values (0, "init", null);
sqlite> select * from proc;
0|init|

how can I allow null foreign key in sqlite when PRAGMA foreign_keys=ON? or it is not possible at all?

Author:Rupesh,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/16304971/is-it-allowed-to-have-null-foreign-key-in-sqlite-when-pragma-foreign-keys-on
CL. :

\nThe ID column is named pid, not id.\nThe parent key column must have a UNIQUE or PRIMARY KEY constraint.\n",
2013-04-30T17:13:51
yy