Fix id->ext_id, moved SQL in right place