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), filename TEXT NOT NULL, content_type TEXT NOT NULL, size_bytes BIGINT NOT NULL, sha256 CHAR(64) NOT NULL, object_key TEXT NOT NULL UNIQUE, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); CREATE INDEX attachments_chat_idx ON attachments(chat_id); CREATE TABLE message_attachments ( message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE, attachment_id UUID NOT NULL REFERENCES attachments(id), PRIMARY KEY(message_id,attachment_id) ); ALTER TABLE messages DROP CONSTRAINT messages_text_not_blank;