-- 创建用户本地模型权限表
-- 用于存储用户对本地模型的访问权限设置
-- 需求: 本地模型权限管理功能

CREATE TABLE aigcspace.user_local_model_permission (
    id SERIAL PRIMARY KEY,
    user_id VARCHAR(50) NOT NULL REFERENCES aigcspace.users(id) ON DELETE CASCADE,
    model_id INTEGER NOT NULL REFERENCES aigcspace.models(id) ON DELETE CASCADE,
    has_access BOOLEAN NOT NULL DEFAULT FALSE,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- 添加字段注释
COMMENT ON TABLE aigcspace.user_local_model_permission IS '用户本地模型权限表';
COMMENT ON COLUMN aigcspace.user_local_model_permission.id IS '主键ID';
COMMENT ON COLUMN aigcspace.user_local_model_permission.user_id IS '用户ID';
COMMENT ON COLUMN aigcspace.user_local_model_permission.model_id IS '模型ID';
COMMENT ON COLUMN aigcspace.user_local_model_permission.has_access IS '是否有权限访问';
COMMENT ON COLUMN aigcspace.user_local_model_permission.created_at IS '创建时间';
COMMENT ON COLUMN aigcspace.user_local_model_permission.updated_at IS '更新时间';

-- 添加索引
CREATE INDEX idx_user_local_model_permission_user_id ON aigcspace.user_local_model_permission(user_id);
CREATE INDEX idx_user_local_model_permission_model_id ON aigcspace.user_local_model_permission(model_id);
CREATE UNIQUE INDEX idx_user_local_model_permission_unique ON aigcspace.user_local_model_permission(user_id, model_id);
