CREATE TABLE IF NOT EXISTS locations_files ( id SERIAL PRIMARY KEY, location_id INTEGER NOT NULL REFERENCES locations_locations(id) ON DELETE CASCADE, original_filename VARCHAR(255) NOT NULL, stored_filename VARCHAR(255) NOT NULL, content_type VARCHAR(150), file_size BIGINT NOT NULL DEFAULT 0, comment TEXT, created_at TIMESTAMP NOT NULL DEFAULT NOW(), deleted_at TIMESTAMP ); CREATE INDEX IF NOT EXISTS idx_locations_files_location ON locations_files(location_id, created_at DESC) WHERE deleted_at IS NULL;