## Schema Mismatch Fix (2026-05-21)

After running `scripts/run_migrations.py`, the app crashed with:
```
psycopg2.errors.UndefinedColumn: column "calc_time" does not exist
```

**Root cause:** `v1_0_init_tables.sql` creates `wcnr_score_history` with `month_date` column, but the dashboard code (`summary_service.py`, `trend_service.py`) expects `calc_time`.

**Fix:**
```python
import psycopg2
conn = psycopg2.connect(host='127.0.0.1', port=54321, database='yfywk', user='ywkuser', password='123')
cursor = conn.cursor()
cursor.execute("ALTER TABLE jcgkzx_monitor.wcnr_score_history ADD COLUMN IF NOT EXISTS calc_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP;")
cursor.execute("UPDATE jcgkzx_monitor.wcnr_score_history SET calc_time = month_date WHERE calc_time IS NULL;")
conn.commit()
```

**Lesson:** Always run `ddl_create_tables.sql` in addition to `scripts/sql/v*.sql` — the DDL has the complete schema.

## YOLO Model Download (2026-05-21)

The app's health check shows 7 missing model files. For testing, download a base YOLO model and rename:

```python
from ultralytics import YOLO
import shutil, os

model = YOLO("yolo26n.pt")  # Downloads from GitHub

target_dir = "model/yolo/production"
os.makedirs(target_dir, exist_ok=True)
shutil.copy("yolo26n.pt", f"{target_dir}/biaochezhajiev2.pt")
shutil.copy("yolo26n.pt", f"{target_dir}/yolov8s-worldv2.pt")
os.remove("yolo26n.pt")
```

This satisfies the health check for the two main YOLO models. Other models (mobileclip, insightface) are optional for face recognition features.

## KingBase Startup Timing

After `docker start kingbasev8`, the database takes 10-15 seconds to be ready. Migration scripts will fail with "database system is starting up" if run too early.

```bash
docker start kingbasev8
sleep 15  # Critical: wait for DB readiness
python scripts/run_migrations.py
```
