HasMany - save association data

No. There are several departments and several companies. I need to add and remove companies from the editing department (not move them). When you first edit, I select the necessary companies and their business_department_id changes, with subsequent saving of the entity, an error occurs.