fixes #2400 add foreign keys to database migrations

The migration will fail if there are orphaned foreign keys.

Note, there is no foreign keys on the database level for polymorphic associations.