Recursive `rowid` in sqlite

90 views Asked by At

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:

  1. transaction begin
  2. insert a preliminary row with c pointing to a dummy value
  3. retrieve the last inserted row id
  4. upgrade the preliminary row to its final form, now with the correct value for c
  5. commit transaction

Is this the right approach?

0

There are 0 answers