Fixed #19607 - prefetch_related crash

Thanks to av@rdf.ru and flarno11@yahoo.de for the report.

Original commit: 4fd94969d859eb25680a5a52ed482c8f22e5ee15