Ich experimentiere gerade ein bisschen mit PostgreSQL und bin dabei mir ein Datenmodell für ein Forum zusammenzubasteln.
Für einen Beitrag habe ich mir die Tabelle "node" ausgedacht:
CREATE TABLE node (
id SERIAL NOT NULL PRIMARY KEY,
person_id INT NOT NULL REFERENCES person(id),
thread_id INT NOT NULL REFERENCES node(id),
parent_id INT REFERENCES node(id),
title VARCHAR(80),
text TEXT,
creation_time TIMESTAMP NOT NULL DEFAULT NOW(),
karma INT NOT NULL DEFAULT 0,
category_id INT NOT NULL REFERENCES category
);
Dabei soll thread_id auf den ersten node des Threads zeigen.
Wenn ich allerdings den ersten node in die Datenbank einfüge, weiß ich ja dessen id noch nicht.
Wenn ich thread_id weglasse, kommt die Fehlermeldung
DBD::Pg::st execute failed: ERROR: null value in column "thread_id" violates not-null constraint
insert() - DBD::Pg::st execute failed: ERROR: null value in column "thread_id" violates not-null constraint
at test.pl line 32
Jetzt könnte ich natürlich das Problem umgehen, indem ich den NOT NULL-constraint einfach rausnehme - aber gibts da keine besser Lösung? Sowas wie 'thread_id INT NOT NULL default id REFERENCES node(id)'?
Diese Variante gibt mir
ERROR: cannot use column references in default expression
Gibt es einen anderen workaround?