I am modelling a user preference system with the following requirements
- a user can have attributes (foods they eat)
- a user can have dynamic categories of preferences
- users which have all their categories with at least one intersect can match
So far I have used Postgres's arrays to build a system to avoid joins. NB. I'm not using proper keys at the moment for simplicity.
CREATE TABLE public.users (
    name text,
    food text[]
);
CREATE TABLE public.preferences (
    name text,
    category text,
    food text[]
);
INSERT INTO public.preferences VALUES ('John', 'Breakfast', '{toast,cereal}');
INSERT INTO public.preferences VALUES ('John', 'Dinner', '{ham}');
INSERT INTO public.preferences VALUES ('Jane', 'Breakfast', '{toast,eggs}');
INSERT INTO public.preferences VALUES ('Jack', 'Breakfast', '{grapefruit}');
INSERT INTO public.users VALUES ('John', '{peas,ham,"ice cream",toast}');
INSERT INTO public.users VALUES ('Jane', '{eggs,ham,"ice cream",cereal,toast}');
INSERT INTO public.users VALUES ('Jack', '{toast,cereal,eggs,peas}');
I have a query that seems to work, however was wondering if anyone had any feedback. It works by asserting that there "are not any categories, which don't match".
select * from users u where name <> 'Jane'
and not exists (select from preferences p where name = 'Jane' and not u.food && p.food)
and not exists (select from preferences p where u.name = p.name
                and not (select u.food from users u where u.name = 'Jane') && p.food);
 name |             food             
------+------------------------------
 John | {peas,ham,"ice cream",toast}
 (1 row)
users.foodandpreferences.food. Doesusers.foodjust track a food to which a user is not allergic (an "acceptable" food)? Or are you trying to capture the difference between "foods a user prefers without association to a category" and "foods a user prefers within a category"? \$\endgroup\$users.foodwill not have a corresponding entry inpreferences.food, or vice versa? \$\endgroup\$