# Quick Fix for Ticket Sync Constraint Error

## The Problem
```
Database error: insert or update on table "tickets" violates foreign key constraint "tickets_assigned_resource_id_fkey"
```

## The Fix (2 Steps)

### Step 1: Run Database Migration
```bash
docker exec pulse-app psql $DATABASE_URL -f /app/migrations/008_relax_tickets_resource_constraints.sql
```

### Step 2: Restart the App (to load new code)
```bash
docker-compose restart app
```

## Test the Fix
```bash
# Trigger a ticket sync
curl -X POST http://localhost:3000/api/sync/entity \
  -H "Content-Type: application/json" \
  -d '{"entities": ["tickets"], "yearsBack": 1, "triggeredBy": "admin"}'

# Watch the logs
docker logs -f pulse-app
```

## What Changed

**Database:**
- Made resource foreign keys deferrable and lenient
- Tickets can now exist even if assigned resource doesn't exist
- Invalid resource IDs are set to NULL instead of causing failure

**Application:**
- Validates resource IDs before inserting tickets
- Logs warnings for tickets with invalid resource references
- Caches valid resource IDs for performance

## Expected Results

✅ Ticket sync completes successfully  
✅ Warnings logged for tickets with invalid resource IDs  
✅ Those tickets have `assigned_resource_id` set to NULL  
✅ All other ticket data is preserved  

## More Details

See `/opt/stacks/pulse/docs/TICKET_SYNC_FIX.md` for complete documentation.
