Fixes #865.
Fixed foreign key generation bug for pgsql.