-- Lab4 attachments: metadata in PostgreSQL, bytes in S3. -- -- ``s3_key`` is server-generated and never exposed through the API. Linkage to -- a message is a many-to-many table so a message can carry ordered attachments -- and the same attachment can be referenced again without duplicating bytes. create table if not exists 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 text not null, status text not null, s3_key text not null, error_code text, created_at timestamptz not null, updated_at timestamptz not null ); create index if not exists attachments_chat_id_idx on attachments (chat_id); create table if not exists message_attachments ( message_id uuid not null references messages (id) on delete cascade, attachment_id uuid not null references attachments (id) on delete cascade, position integer not null, primary key (message_id, attachment_id) ); create index if not exists message_attachments_attachment_idx on message_attachments (attachment_id);