-- Many-to-many join table between thoughts and learnings. -- Allows a learning to reference multiple source thoughts and a thought to -- expose multiple related learnings, queryable in both directions. CREATE SEQUENCE IF NOT EXISTS public.identity_thought_learning_links_id INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 START 1 CACHE 1; CREATE TABLE IF NOT EXISTS public.thought_learning_links ( id bigint NOT NULL DEFAULT nextval('public.identity_thought_learning_links_id'), thought_id bigint NOT NULL REFERENCES public.thoughts(id) ON DELETE CASCADE, learning_id bigint NOT NULL REFERENCES public.learnings(id) ON DELETE CASCADE, relation text NOT NULL DEFAULT 'source', created_at timestamptz NOT NULL DEFAULT now(), CONSTRAINT thought_learning_links_pkey PRIMARY KEY (id), CONSTRAINT thought_learning_links_unique UNIQUE (thought_id, learning_id) ); CREATE INDEX IF NOT EXISTS idx_thought_learning_links_thought_id ON public.thought_learning_links (thought_id); CREATE INDEX IF NOT EXISTS idx_thought_learning_links_learning_id ON public.thought_learning_links (learning_id);