Fixed foreign keys bugs