Fix foreign key type inference

Fixes #852 and #1130