A federal judge on Wednesday ordered the Trump administration to stop denying asylum to anyone who transits through another country to reach the U.S. border, marking the latest legal defeat for a president waging an all-out battle to stem the flow of migrants entering from Mexico.

The ruling by U.S. District Judge Jon Tigar in San Francisco came hours after another federal judge in Washington, D.C., let the nine-day-old policy stand. The California judge’s preliminary injunction halts the policy while the lawsuit plays out in court.

The new policy denies asylum to anyone who passes through another country on the way to the U.S. without seeking protection there. Most crossing the Mexican border are from Central America, but it would apply to all nationalities except countries that border the U.S.

The dramatic change went into effect last week, though there were conflicting reports on whether U.S. immigration agencies were enforcing it.

Top U.S. officials said the policy would discourage migrants from leaving their countries, which they say is necessary to reduce the numbers of people that U.S. authorities are detaining.

Tigar, who was appointed by former President Barack Obama, halted another Trump policy last year to deny asylum to people who crossed the border illegally.

The judge said the policy could expose migrants to violence and abuse, deny their rights under international law and return them to countries they were fleeing. He cited the administration’s own court filings to argue that Mexico was unsafe.

Tigar acknowledged that the U.S. immigration system is overwhelmed by the surge in migrants from Central America over the last year. “But shortcutting the law, or weakening the boundary between Congress and the Executive, are not the solutions to these problems,” he wrote.

The Justice and Homeland Security Departments did not immediately respond to requests for comment.

President Donald Trump told reporters before his departure for a fundraiser in West Virginia that the decision earlier Wednesday by U.S. District Judge Timothy Kelly, a Trump appointee, was a “tremendous ruling.” He added: “We appreciate it. We respect the courts very much. That helps us very much at the border.”