Refactored render_constraints_query to be simpler