Invalid SQL could be created by sorting on an invalid alias, with a field that exists on the model. Fixes #3797