I have the following tables:
CREATE TABLE "Abilities" (
"ID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
"Name" TEXT NOT NULL,
"Description" TEXT NOT NULL
)
and
CREATE TABLE "Creatures" (
"ID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
"Faction" TEXT,
"Name" TEXT NOT NULL UNIQUE,
"Tier" INTEGER NOT NULL DEFAULT 1,
"Upgrade" INTEGER NOT NULL DEFAULT 0,
"GoldCost" INTEGER NOT NULL DEFAULT 0,
"PopulationWeekly" INTEGER NOT NULL DEFAULT 0,
"Attack" INTEGER NOT NULL DEFAULT 0,
"Defense" INTEGER NOT NULL DEFAULT 0,
"DamageMin" INTEGER NOT NULL DEFAULT 0,
"DamageMax" INTEGER NOT NULL DEFAULT 0,
"Initiative" INTEGER NOT NULL DEFAULT 0,
"Speed" INTEGER NOT NULL DEFAULT 0,
"Health" INTEGER NOT NULL DEFAULT 0,
"Mana" INTEGER NOT NULL DEFAULT 0,
"Shots" INTEGER NOT NULL DEFAULT 0,
"Experience" INTEGER NOT NULL DEFAULT 0,
"PowerRating" INTEGER NOT NULL DEFAULT 0,
"Abilities" TEXT,
FOREIGN KEY("Abilities") REFERENCES "Abilities"("Name")
)
When trying to do the following insert:
INSERT INTO "main"."Creatures"
("Faction", "Name", "Abilities")
VALUES ('Academy', 'Gremlin', null);
I get a:
Result: foreign key mismatch - "Creatures" referencing "Abilities"
At line 1:
INSERT INTO "main"."Creatures"
("Faction", "Name", "Abilities")
VALUES ('Academy', 'Gremlin', null);
error. But I thought it would be OK if I try to insert a 'null' into a foreign key? In this case I want to be able to insert empty values for some "Ability" values, since not all records must have an "Ability" value.
How do I make it so that whenever something is inserted into "Creatures" SQL would check if "Ability" IS NOT NULL then the record must exist in the "Name" column of the "Abilities" table.