Fixes for a couple of regression in generic foreign key handling

Fixed tickets #22992 and #22994.

Should be backpatched to 1.7.
Good point, I'll check this.