Files
triqura-ecd/supabase/migrations/20251123_fix_reports_created_by_constraint.sql
colinislit 371021d3c0 fix: remove reports created_by FK constraint for prototype
Issue: Foreign key constraint violation when creating reports
- reports.created_by referenced practitioners.id
- Code was storing auth.uid() instead of practitioner.id
- Demo practitioners have user_id: null (no auth link)

Solution (prototype):
- Drop FK constraints on created_by and updated_by
- Store auth user ID directly without constraint
- Added migration: 20251123_fix_reports_created_by_constraint.sql

Note: In production, populate practitioners.user_id and restore
FK constraints for proper data integrity.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-23 21:42:46 +01:00

20 lines
825 B
SQL

-- Migration: Fix reports created_by constraint for prototype
-- Remove foreign key constraint to practitioners table
-- For prototype: store auth user ID directly without FK constraint
-- Drop the foreign key constraints
ALTER TABLE reports
DROP CONSTRAINT IF EXISTS reports_created_by_fkey;
ALTER TABLE reports
DROP CONSTRAINT IF EXISTS reports_updated_by_fkey;
-- Add comments for clarity
COMMENT ON COLUMN reports.created_by IS 'Auth user ID (from auth.users) - no FK constraint in prototype';
COMMENT ON COLUMN reports.updated_by IS 'Auth user ID (from auth.users) - no FK constraint in prototype';
-- Note: In production, you would:
-- 1. Ensure all practitioners have user_id populated
-- 2. Look up practitioner.id from practitioners WHERE user_id = auth.uid()
-- 3. Restore FK constraints for data integrity