I have next table
create table use_flags3 (
id INTEGER,
flag_name VARCHAR NOT NULL,
flag_description VARCHAR NOT NULL,
flag_type_id INTEGER NOT NULL,
package_id INTEGER,
FOREIGN KEY (flag_type_id) REFERENCES use_flags_types(id),
FOREIGN KEY (package_id) REFERENCES packages(id),
PRIMARY KEY (id)
);
I need flag_name
column to be unique only when flag_type_id equals to 1. I tried to achieve this with next constraint
CONSTRAINT idx1_chk CHECK (
flag_type_id in (select id from use_flags_types where flag_type="local") or
flag_type_id in (select id from use_flags_types where flag_type="expand") or
flag_type_id in (select id from use_flags_types where flag_type="expand_hidden") or
(
flag_type_id in (select id from use_flags_types where flag_type="global") and
flag_name not in (select flag_name from use_flags)
)
)
sqlite says 'subqueries prohibited in CHECK constraints'. I can replace
flag_type_id in (select id from use_flags_types where flag_type="local")
with
flag_type_id = ${ID_HERE} -- id from `select id from use_flags_types where flag_type="local"`
but I can not do same trick for 2nd subpart of the last part of constraint
flag_name not in (select flag_name from use_flags)
Is there any chance to do what I originally want within one table (I would really don't like to split those data in 2(+) tables)?
// hope description is quite clear