I have the following 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 a column flag_nameto be unique only when flag_type_id is 1. I tried to achieve this with the following restriction
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 that "subqueries are not allowed 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’t do the same trick for the second part of the last part of the restriction
flag_name not in (select flag_name from use_flags)
Is there a chance to do what I originally wanted in one table (I really would not want to split this data into 2 (+) tables)?
// the description of hope is pretty clear
source
share