Simple generic foreign key support

Contains @hynekcer's commits from #45