Fix foreign key issues

Add missing :dependent to all associations not to left orphan
records behind causing foreign key constraint issues
Merge pull request #3160 from pitr-ch/bug/996685

Fix foreign key issues