Fixes for a couple of regression in generic foreign key handling

Fixed tickets #22992 and #22994.

Should be backpatched to 1.7.
should a virtual field that does exist also be tested? (or is it tested elsewhere?)