fixes #2400 migration to remove orphaned records and add foreign keys co...

...nstraints to database tables