Fix foreign key type inference

Fixes #852 and #1130
Merge pull request #1544 from seegno/fix-fk-type-inference

Fix foreign key type inference