-- # 书籍作者关联表
--
-- ## 基本信息
--
-- 模块：book
-- 表名：book_author_relation
-- 模型：model/book/book_author_relation.go
-- 迁移接入：initialize/gorm_biz.go
-- 删除策略：硬删表
-- 职责：承载书籍与作者的多对多关系及展示顺序，保证同一本书下作者关联唯一。

CREATE TABLE book_author_relation (
  id bigserial PRIMARY KEY,
  created_at timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
  book_id bigint NOT NULL,
  author_id bigint NOT NULL,
  author_sort integer NOT NULL DEFAULT 1 CHECK (author_sort > 0)
);

COMMENT ON TABLE book_author_relation IS '书籍作者关联表';
COMMENT ON COLUMN book_author_relation.id IS '主键';
COMMENT ON COLUMN book_author_relation.created_at IS '创建时间';
COMMENT ON COLUMN book_author_relation.updated_at IS '更新时间';
COMMENT ON COLUMN book_author_relation.book_id IS '关联书籍 ID';
COMMENT ON COLUMN book_author_relation.author_id IS '关联作者 ID';
COMMENT ON COLUMN book_author_relation.author_sort IS '作者展示顺序';

CREATE UNIQUE INDEX uk_book_author_relation_book_id_author_id ON book_author_relation (book_id, author_id);
CREATE INDEX idx_book_author_relation_author_id ON book_author_relation (author_id);
CREATE INDEX idx_book_author_relation_book_id_author_sort ON book_author_relation (book_id, author_sort);
