Check for foreign key with a null value for belongsTo relations...

... to avoid the creation of unnecessary query that obviously returns an empty result. Fixes #4371.