I have the following setup of tables:
CREATE TABLE public.tags (
tag_id int4 NOT NULL,
creation_timestamp timestamp NULL,
"name" varchar(255) NULL,
CONSTRAINT tags_pkey PRIMARY KEY (tag_id)
);
-- public.tag_targets definition
-- Drop table
-- DROP TABLE public.tag_targets;
CREATE TABLE public.tag_targets (
id int4 NOT NULL,
creation_timestamp timestamp NULL,
target_id int8 NULL,
target_name varchar(255) NULL,
last_update_timestamp timestamp NULL,
tag_id int4 NULL,
CONSTRAINT tag_targets_pkey PRIMARY KEY (id),
CONSTRAINT fkcesi55mqvysjv63c1xf2j15oh FOREIGN KEY (tag_id) REFERENCES tags(tag_id)
);
I am trying to run the following query:
SELECT *
FROM tag_targets tt, tags t
WHERE tt.tag_id = t.tag_id
AND (t."name" IN ('Keeper', 'Pk'))
GROUP by tt.target_id
However it wants the PK of both Tags and Tagtarget in the group by:
ERROR: column "tt.id" must appear in the GROUP BY clause or be used in an aggregate function
Is there anyway to group on the target_id column? Also feel free to give any feedback on table design as I went for a generic mapping table and independent tags table