Files
akmon/doc_zhipao/ANALYTICS_FUNCTION_CONFLICT_FIX.md
2026-01-20 08:04:15 +08:00

115 lines
2.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 快速修复 Analytics RPC 函数冲突问题
## 🚨 错误原因
```
ERROR: 42P13: cannot change return type of existing function
HINT: Use DROP FUNCTION get_recent_activities(uuid,integer) first.
```
这个错误表明数据库中已经存在同名但返回类型不同的函数。
## ✅ 解决方案
### 步骤 1: 使用修复版 SQL 文件
执行 `analytics_rpc_fixed.sql` 文件,该文件会:
1. **先删除所有可能存在的旧函数**
2. **重新创建所有函数**
3. **验证创建结果**
### 步骤 2: 在 Supabase SQL Editor 中执行
```sql
-- 复制 analytics_rpc_fixed.sql 中的所有内容到 Supabase SQL Editor
-- 然后点击执行
```
### 步骤 3: 验证函数创建成功
执行后应该看到类似这样的消息:
```
NOTICE: 成功创建 4 个函数
NOTICE: ✅ 所有 Analytics RPC 函数创建成功!
```
### 步骤 4: 检查函数列表
```sql
-- 验证函数是否正确创建
SELECT
routine_name,
routine_type,
data_type
FROM information_schema.routines
WHERE routine_schema = 'public'
AND routine_name IN ('get_teacher_analytics', 'get_top_performers', 'get_chart_data', 'get_recent_activities')
ORDER BY routine_name;
```
应该看到 4 个函数都返回 `jsonb` 类型。
## 🔧 主要修复内容
### 1. 明确删除旧函数
```sql
DROP FUNCTION IF EXISTS public.get_recent_activities(uuid, integer);
-- 删除所有可能存在的旧版本函数
```
### 2. 重命名参数避免冲突
```sql
-- 原来
"limit" integer DEFAULT 20
-- 修改为
activity_limit integer DEFAULT 20
```
### 3. 重命名 chart_type 参数
```sql
-- 原来使用了保留字 "type"
"type" text DEFAULT 'completion_rate'
-- 修改为
chart_type text DEFAULT 'completion_rate'
```
### 4. 统一返回类型
所有函数都返回 `jsonb` 类型,确保一致性。
## 🧪 测试函数
创建成功后,可以测试函数调用:
```sql
-- 测试统计数据
SELECT public.get_teacher_analytics();
-- 测试优秀学员
SELECT public.get_top_performers();
-- 测试图表数据
SELECT public.get_chart_data();
-- 测试近期活动
SELECT public.get_recent_activities();
```
## 📱 前端调用
修复后Analytics 页面应该能正常调用:
```vue
<supadb rpc="get_teacher_analytics" :params="analyticsParams" />
<supadb rpc="get_top_performers" :params="performersParams" />
<supadb rpc="get_chart_data" :params="chartParams" />
```
## ⚠️ 注意事项
1. **完全执行**: 确保整个 SQL 文件都执行完成
2. **权限检查**: 确认函数权限正确授予
3. **错误监控**: 查看执行过程中是否有其他错误
执行成功后Analytics 页面将能够正常加载并显示测试数据。