| -- Complete fix for species table INSERT issues | |
| -- Run this in your Supabase SQL Editor | |
| -- ============================================================ | |
| -- STEP 1: Check current RLS policies | |
| -- ============================================================ | |
| SELECT | |
| schemaname, | |
| tablename, | |
| policyname, | |
| permissive, | |
| cmd as operation, | |
| qual as using_expression, | |
| with_check | |
| FROM pg_policies | |
| WHERE tablename = 'species' | |
| ORDER BY policyname; | |
| -- ============================================================ | |
| -- STEP 2: Drop ALL existing policies on species table | |
| -- ============================================================ | |
| DROP POLICY IF EXISTS "Species are viewable by everyone" ON public.species; | |
| DROP POLICY IF EXISTS "Allow authenticated inserts to species" ON public.species; | |
| DROP POLICY IF EXISTS "Allow service role to insert species" ON public.species; | |
| DROP POLICY IF EXISTS "Allow inserts to species" ON public.species; | |
| DROP POLICY IF EXISTS "Anyone can insert species" ON public.species; | |
| -- ============================================================ | |
| -- STEP 3: Create new policies | |
| -- ============================================================ | |
| -- Allow everyone to read species data | |
| CREATE POLICY "Enable read access for all users" | |
| ON public.species FOR SELECT | |
| USING (true); | |
| -- Allow anyone to insert new species (for the API) | |
| CREATE POLICY "Enable insert access for all users" | |
| ON public.species FOR INSERT | |
| WITH CHECK (true); | |
| -- ============================================================ | |
| -- STEP 4: Verify RLS is enabled | |
| -- ============================================================ | |
| ALTER TABLE public.species ENABLE ROW LEVEL SECURITY; | |
| -- ============================================================ | |
| -- STEP 5: Verify the policies were created | |
| -- ============================================================ | |
| SELECT | |
| schemaname, | |
| tablename, | |
| policyname, | |
| permissive, | |
| cmd as operation, | |
| with_check | |
| FROM pg_policies | |
| WHERE tablename = 'species' | |
| ORDER BY policyname; | |
| -- ============================================================ | |
| -- STEP 6: Test insert manually | |
| -- ============================================================ | |
| -- Try inserting a test record to verify it works | |
| -- INSERT INTO public.species (sno, species_name, species_type, location, latitude, longitude, added_by, notes) | |
| -- VALUES (9999, 'Test Species', 'plant', 'QSC', 12.92000, 80.12100, 'test', 'Test insert'); | |
| -- | |
| -- -- If successful, delete the test record | |
| -- DELETE FROM public.species WHERE sno = 9999; | |