CREATE TABLE attachments ( id UUID PRIMARY KEY, chat_id UUID NOT NULL REFERENCES chats (id), owner_id UUID NOT NULL REFERENCES users (id), filename TEXT NOT NULL, content_type TEXT NOT NULL, size_bytes BIGINT NOT NULL, sha256 TEXT NOT NULL, object_key TEXT NOT NULL UNIQUE, status TEXT NOT NULL, error_code TEXT, created_at TIMESTAMPTZ NOT NULL, updated_at TIMESTAMPTZ NOT NULL ); CREATE INDEX attachments_chat_id_idx ON attachments (chat_id); CREATE TABLE message_attachments ( message_id UUID NOT NULL REFERENCES messages (id), attachment_id UUID NOT NULL REFERENCES attachments (id), position INT NOT NULL, PRIMARY KEY (message_id, attachment_id) ); CREATE INDEX message_attachments_attachment_id_idx ON message_attachments (attachment_id);