Fixed sql join generation for postgress database