Use transactions for scanning many-to-many relations
Fix bug where ids were not being assigned on many-to-many relations