I have a sqlite table t
with two columns: a ROWID
primary key column called id
and a column c
which is a foreign key to t
itself:
CREATE TABLE t (
id INTEGER PRIMARY KEY,
c INT NOT NULL,
FOREIGN KEY (c) REFERENCES t(id));
Inserts are straightforward as long as c
refers to a row already inserted, but I'm not sure how to insert a row whose c
refers to that row itself. Is this possible in sqlite?
I know that SQLite is able to return the last inserted row id, so I could hack something together like:
- transaction begin
- insert a preliminary row with
c
pointing to a dummy value - retrieve the last inserted row id
- upgrade the preliminary row to its final form, now with the correct value for
c
- commit transaction
Is this the right approach?