German Chancellor Angela Merkel has said building walls will not solve problems caused by immigration, during a visit to Mexico City.
Mrs Merkel did not mention US President Donald Trump, who has vowed to erect a barrier across the Mexican border.
She said history showed that migration pressures were resolved only when empires got on well with their neighbours.
Mrs Merkel grew up in communist East Germany, behind the Iron Curtain.
Addressing a panel discussion with Mexico's President Enrique Pena Nieto, she said: "Obviously the main reason for people leaving must be addressed on site first, which means that putting up walls and cutting oneself off will not solve the problem."
Mrs Merkel suggested the key to managing large-scale migration was to improve living standards and opportunities in affected areas.
The German leader has sometimes faced criticism at home for welcoming more than a million refugees since 2015.
In another counter to Mr Trump, Mrs Merkel backed Mexico's free trade position ahead of talks on renegotiating the North American Free Trade Agreement (Nafta) between Mexico, the US and Canada.
Mr Trump has repeatedly criticised the agreement, and threatened to withdraw from it if he cannot renegotiate it in America's favour.