CREATE TABLE attachments ( id uuid PRIMARY KEY, chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE, owner_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE, filename text NOT NULL CHECK (char_length(filename) BETWEEN 1 AND 255), content_type text NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg')), size_bytes integer NOT NULL CHECK (size_bytes >= 1 AND size_bytes <= 10485760), sha256 text NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'), status text NOT NULL CHECK (status IN ('ready')), s3_key text NOT NULL UNIQUE, error_code text NULL, created_at timestamptz NOT NULL, updated_at timestamptz NOT NULL ); CREATE INDEX attachments_chat_id_idx ON attachments (chat_id); CREATE INDEX attachments_created_at_idx ON attachments (created_at);