Skip to main content

post_images

Overview

The post_images table supports multiple image attachments per post. This replaced the earlier single image_url column on the posts table. Images are ordered by position and store both the storage URL and optional alt text.

Schema

-- Pre-existing table (inferred from frontend code)
CREATE TABLE post_images (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
post_id UUID REFERENCES posts(id) ON DELETE CASCADE NOT NULL,
image_url TEXT NOT NULL,
position INTEGER DEFAULT 0,
alt_text TEXT,
created_at TIMESTAMPTZ DEFAULT NOW()
);

Columns

ColumnTypeNullableDefaultDescription
iduuidNouuid_generate_v4()Primary key
post_iduuidNo--Parent post, cascades on delete
image_urltextNo--URL to image in Supabase storage
positionintegerNo0Display order (0-based)
alt_texttextYes--Accessibility description
created_attimestamptzNoNOW()Upload timestamp

RLS Policies

-- SELECT: Same visibility as parent post
CREATE POLICY "Users can view images on accessible posts"
ON post_images FOR SELECT
USING (EXISTS (
SELECT 1 FROM posts WHERE posts.id = post_images.post_id
AND can_view_post(auth.uid(), posts.id)
));

-- INSERT: Post author can add images
CREATE POLICY "Users can add images to own posts"
ON post_images FOR INSERT
WITH CHECK (EXISTS (
SELECT 1 FROM posts WHERE posts.id = post_images.post_id
AND posts.user_id = auth.uid()
));

-- DELETE: Post author can remove images
CREATE POLICY "Users can delete images from own posts"
ON post_images FOR DELETE
USING (EXISTS (
SELECT 1 FROM posts WHERE posts.id = post_images.post_id
AND posts.user_id = auth.uid()
));

Last updated: 2026-02-07