Fixed #14334 -- Query relation lookups now check object types.

Thanks rpbarlow for suggestion.

Thanks loic, akaariai and jorgecarleitao for review.