SQLite Forum

Non primary key alias to rowid
Login
Aha!  I understand your conundrum now after looking at the sessions module more closely.  So what you want is to be able to define a column that "acts like" it were the INTEGER PRIMARY KEY, or RowID, but is not.

You can do this with triggers and ignore the RowID entirely.  Example:

```
CREATE TABLE IF NOT EXISTS translatables
(
     id                INTEGER UNIQUE, 
     translationTextId TEXT PRIMARY KEY NOT NULL CHECK (translationTextId != ''),
     screenId          TEXT,
     defaultText       TEXT
);

CREATE TRIGGER translatables AFTER INSERT ON translatables FOR EACH ROW WHEN id IS NULL
BEGIN
   UPDATE translatables 
      SET id = coalesce((select max(id) + 1 from translatables), 1)
     FROM translatables
    WHERE rowid == new.rowid;
END;
```

So when you try to insert a record in the table with no "id" the id will be set to the value one greater than the current max value of id in the table, starting with 1 for the 1st row.  (Just the same as RowID).  However it is NOT the RowID, so you cannot refer to it as such, nor can you expect to get the value by using `last_insert_rowid()`, for example.  You will have to actually retrieve the value of the ID after inserting records.  It will however be stable and usable in foreign key constraints.