Files
ai-showcase-platform/DATABASE_MIGRATION_README.md

75 lines
2.0 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.

# 資料庫遷移說明
## 問題描述
競賽管理系統在創建競賽時出現以下錯誤:
1. `competition_award_types` 表缺少 `order_index` 欄位
2. `competition_teams` 表缺少 `registered_at` 欄位
3. 外鍵約束失敗,存在孤立的關聯記錄
## 解決方案
### 方法一:使用智能檢查腳本(推薦)
```bash
mysql -u your_username -p your_database_name < add-missing-columns.sql
```
### 方法二:使用簡化腳本
```bash
mysql -u your_username -p your_database_name < add-missing-columns-simple.sql
```
## 腳本內容
### 1. 添加缺失欄位
-`competition_award_types` 表添加 `order_index` 欄位
-`competition_teams` 表添加 `registered_at` 欄位
### 2. 清理孤立記錄
- 刪除所有關聯表中不存在的 `competition_id` 記錄
### 3. 添加必要索引
- 為所有關聯表添加適當的索引以提升查詢性能
## 執行前注意事項
1. **備份資料庫**
```bash
mysqldump -u your_username -p your_database_name > backup_before_migration.sql
```
2. **確認資料庫名稱**
- 將 `your_database_name` 替換為實際的資料庫名稱
- 將 `your_username` 替換為實際的用戶名
3. **檢查權限**
- 確保用戶有 ALTER TABLE 和 DELETE 權限
## 執行後驗證
執行完成後,可以運行以下查詢來驗證:
```sql
-- 檢查欄位是否添加成功
DESCRIBE competition_award_types;
DESCRIBE competition_teams;
-- 檢查索引是否創建成功
SHOW INDEX FROM competition_award_types;
SHOW INDEX FROM competition_teams;
-- 檢查孤立記錄是否已清理
SELECT COUNT(*) as orphaned_judges FROM competition_judges
WHERE competition_id NOT IN (SELECT id FROM competitions);
```
## 如果遇到錯誤
如果執行過程中遇到 "column already exists" 或 "index already exists" 錯誤,這是正常的,表示該欄位或索引已經存在,可以忽略這些錯誤。
## 聯繫支援
如果遇到其他問題,請檢查:
1. MySQL 版本是否支援所使用的語法
2. 用戶權限是否足夠
3. 資料庫連接是否正常