Fix SQL bug in model code;