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;
I would suggest to use
nullvalues in the columnparentfor root nodes, because this way all you have to do is add referential integrity to your table.Add a column
iddefined asINTEGER PRIMARY KEY, so that it is an alias of therowidand also make the columnparentto referenceid:Now, turn on foreign key support:
and insert rows:
This will fail:
because there is no row in the table with
id = 5.See the demo.