issue 1615: delete removed relations before adding

Deleting removed relations before adding new ones can prevent duplicate key errors. Fixes issue #1615.