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 9-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.”

The California judge ruled in favor of advocacy groups represented by the American Civil Liberties Union, Southern Poverty Law Center and Center for Constitutional Rights.

Melissa Crow, an attorney for the Southern Poverty Law Center, said the decision was “an important victory for incredibly vulnerable individuals and families from besieged Central American countries seeking refuge in our country.”

”We will continue to fight this draconian policy as well as the myriad of others through which the Trump administration continues to wage war on asylum-seekers and our nation’s asylum system,” Crow said.

The policy would had limited exceptions that would allow for asylum: If someone has been trafficked, if an asylum seeker sought protection in a country but was denied or if the country the migrant passed through did not sign one of the major international treaties that govern how refugees are managed — though most Western countries have signed them.

The decision came as tens of thousands of people are waiting in Mexico on official and unofficial lists formed after U.S. agents started turning away many asylum seekers, citing lack of space and delays in immigration courts. They also include people are forced to wait in Mexico while their cases wind through U.S. immigration policy, another Trump policy that has so far survived a legal challenge.

Tigar’s ruling is the latest example of courts dealing Trump a setback on immigration policy. A court stopped the administration from detaining asylum seekers without giving them a chance to be released on bond.

A judge in Oakland, California, prevented the Trump administration from tapping $2.5 billion in Pentagon money to build border walls. The administration has appealed that decision to the U.S. Supreme Court and asked for a ruling by Friday.