Foreign keys TO a virtual table
(1) By anonymous on 2022-04-18 09:55:00 [link] [source]
I have units like kg
litre
(and many more) that I have defined as enums in my codebase. I'm trying to expose these as a constants
table in sqlite via virtual tables.
I'm able to query select id, code from constants
correctly and see all the data.
However I'm not able to add a foreign key that points to this virtual table. This sql fails:
create table item (
qty integer,
unit_id integer references constants(id)
);
insert into item (qty) values (5);
Is there something I'm doing wrong is is this simply not supported and I should avoid adding foreign keys to virtual tables?
(2) By Richard Hipp (drh) on 2022-04-18 10:32:52 in reply to 1 [link] [source]
It is logically inconsistent to allow a foreign key on a virtual table.
The whole point of virtual tables is that they do not contain data that is under the control of SQLite. That means that the contents of the virtual table can change without SQLite being aware. If there is a foreign key constraint on that table, and the table changes, there is no way for SQLite to know about this and raise an error if appropriate.
(3) By anonymous on 2022-04-18 11:24:49 in reply to 2 [source]
Thank you so much for clearing that up. You saved me a lot of time :D