President Donald Trump says trade wars are easy to win, but that hasn’t always been true in U.S. history. For the first 40 years of the republic, the Founders struggled to establish international trade agreements that Americans would find acceptable. The need for trade leverage was the first factor motivating James Madison to call for a new Constitution. And trade wars had a way of turning into shooting wars.

The War of 1812, the first declared war in U.S. history, was the result of a trade fight that the Americans seemed unable to win with economic sanctions alone.

In the 18th century, empires worked much as multinational free-trade zones do in the modern world. Different parts of the British Empire could trade freely with one another, but not with the French or Spanish empires.

So when the U.S. declared independence in 1776, the Founders were undertaking a kind of proto-Brexit. The British, deeply displeased, cut off American access to British ports. After the Revolutionary War, the U.S. found itself struggling to regain access.

“What is to be done?” Madison asked James Monroe rhetorically in a letter he wrote in April 1785. “Must we remain passive victims to foreign politics; or shall we exert the lawful means which our independence has put into our hands, of extorting redress?” Madison was proposing “retaliating regulations of trade”: in short, a trade war to force the British to allow American shipping to British ports.

The problem was that under the Articles of Confederation, it was almost impossible to coordinate a single national trade policy. Individual states could deviate from tariffs or export sanctions.

Madison’s solution to the trade war problem was to design a new, more effective government. “I conceive it to be of great importance that the defects of the federal system should be amended,” Madison wrote. The states “cannot long respect a government which is too feeble to protect their interests.”

A few months later, Madison and the Virginia assembly proposed a convention to discuss “the subject of general regulations” of trade. That would become the Annapolis, Maryland, convention of 1786, which in turn proposed the Philadelphia convention of the following year. It’s no exaggeration to say that Madison’s ideas for the Constitution were born from trade troubles.

Once the Constitution was ratified and the new federal government was in place, however, it turned out to be extremely hard to achieve the results the Founders wanted. As the British-French wars that broke out with the French Revolution bled into the Napoleonic wars, both sides barred U.S. shipping.

As President Thomas Jefferson’s secretary of state, Madison returned to his old ideas about trade wars. First, he proposed the famous embargo of 1807, a game of chicken in which the U.S. banned all exports of any kind in the hopes of bringing the British to their knees. It failed. Americans’ ability to live without export revenue ran out before the British blinked.

Madison was elected president anyway in 1808. He spent the next four years trying every imaginable configuration of trade sanction against Britain and France, trying to play them against each other. His goal was, as always, to restore access to foreign ports.

The peaceful nature of Madison’s efforts went against the conventional wisdom of the time, which held that only military force could eventually produce meaningful trade concessions. Madison intended to prove the conventional wisdom wrong. He was confident partly because he had designed a successful Constitution that itself contradicted many accepted ideas about what a republic could or could not do.

But Madison also thought he had no choice. The U.S. didn’t have a standing army, and its minuscule navy consisted of just six frigates, not all of them operational.

This was by design. As a republican, Madison believed that a standing army or navy would become tools to facilitate the end of democratic government and its replacement by empire, as had happened in ancient Rome.

Madison gradually came to believe that his theories about trade war were overstated — and he began to threaten military action against Great Britain. Everyone understood that would take the form of an invasion of Canada, from which Britain supplied its West Indian colonies.

Ultimately, Madison thought he had to go to Congress and ask for a declaration of war against Britain. The War of 1812 was born of the American inability to achieve its trade goals using trade sanctions.

Ironically, Madison’s strategy of threat-plus-sanctions worked: The British revoked their standing orders to ban and capture U.S. trade in June 1812.

But the decision came too late to avert war, which had been declared in Washington at almost the exact same time as the British had caved.

Today’s U.S. is, of course, a global superpower little like the republic of 1812. But the lessons of the framers’ trade wars are still relevant: Trade conflicts don’t always work in practice the way they do in theory. And when push comes to shove, trade strife can lead to armed conflict.

The War of 1812 was a near-failure. The U.S. was lucky to get out with a draw. That’s worth remembering as Trump commences alienating U.S. allies over trade.

Noah Feldman is a professor of constitutional and international law at Harvard University and was a clerk to U.S. Supreme Court Justice David Souter. His seven books include “The Three Lives of James Madison: Genius, Partisan, President” and “Cool War: The Future of Global Competition.”