I'd like to express:
"insertion of record with 'parent' value that is not included in 'rowid' AFTER INSERTION is forbidden."
My intention is to keep the table internally consistent as a directed acyclic graph, with every record being a node referring to its parent (root nodes are their own parent). How can I do that?
Here's what I have (with rowid used as the primary key):
CREATE TABLE Heap (
name TEXT CHECK(typeof(name) = 'text')
NOT NULL
UNIQUE ,
parent INTEGER DEFAULT rowid ,
color INTEGER CHECK(color BETWEEN 0 AND 2)
);
CREATE TRIGGER parent_not_in_rowid
BEFORE INSERT ON Heap
BEGIN
SELECT RAISE(FAIL, 'parent id inconsistent') FROM Heap
WHERE NOT EXISTS(SELECT 1 FROM Heap WHERE NEW.rowid = NEW.parent);
END;