-- 184_study_topic_documents.sql (6/6) -- 자료(library document) ↔ 주제 다대다 매핑. -- 한 자료가 여러 주제에 속할 수 있다 (예: "안전관리론" 교재가 가스기사와 산업안전기사 양쪽에 속함). -- -- user_id 는 권한 체크용 반정규화 — 다른 사용자의 자료를 묶지 못하도록 강제. -- documents 본체는 손대지 않고 본 조인 테이블만 사용. -- -- 향후 자산 타입(audio/vocab/question_set 등)이 늘어나면 polymorphic 테이블 대신 -- study_topic_audio_assets / study_topic_vocab_decks 등 타입별 조인 테이블을 추가한다. -- (study_topic_items 같은 단일 polymorphic 테이블은 만들지 않는다.) CREATE TABLE IF NOT EXISTS study_topic_documents ( study_topic_id BIGINT NOT NULL REFERENCES study_topics(id) ON DELETE CASCADE, document_id BIGINT NOT NULL REFERENCES documents(id) ON DELETE CASCADE, user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE, sort_order INTEGER NOT NULL DEFAULT 0, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), PRIMARY KEY (study_topic_id, document_id) );