The UK government's law officer for Scotland, Lord Keen, has resigned amid a legal row over Brexit legislation.
BBC Scotland understands the advocate general found it difficult to reconcile plans to override the Brexit withdrawal agreement with existing laws.
Lord Keen's resignation was initially not accepted by Downing Street, with Prime Minister Boris Johnson saying that conversations were ongoing.
However it was later confirmed that he has left his post.
The government's most senior lawyer - Sir Jonathan Jones, permanent secretary to the government legal department - had already resigned over the Internal Markets Bill, which passed its first parliamentary test on Monday.
Northern Ireland Secretary Brandon Lewis has told MPs the legislation could "break international law" by overriding the withdrawal agreement signed with the EU.
Lord Keen subsequently said in the House of Lords that his view was that "the bill does not of itself constitute a breach of international law or of the rule of law".
He said that Mr Lewis had "essentially answered the wrong question".
But the Northern Ireland secretary later doubled down, saying that his statement was a "very straight answer" which was "absolutely in line" with legal advice.
Media playback is unsupported on your device
BBC Political Editor Laura Kuenssberg said Lord Keen's resignation had been rumoured for days, and had ultimately been submitted "after Brandon Lewis contradicted him this morning".
The prime minister said discussions about the matter were "continuing" on Wednesday afternoon when he appeared before the Commons Liaison Committee.
But shortly after the meeting concluded, a Downing Street spokesman confirmed that the advocate general had left his post and thanked him for his service.
The BBC understands Mr Johnson has already struck a deal with potential Tory rebels over the legislation, which would give MPs "an extra layer of parliamentary oversight".
Richard Keen had been the chairman of the Scottish Conservatives until being appointed as Advocate General in 2015 - when he was also made a life peer as Baron Keen of Elie.
The QC has represented the UK government in court in a number of high-profile cases, including over the prorogation of parliament in 2019 and the "Article 50" Brexit case in 2016-17.
As a law officer in the UK government, Lord Keen had been defending plans to give ministers powers to change the EU withdrawal agreement in the event that the two sides can't agree a trade deal.
He argued that while this part of the UK internal market bill creates tension with the UK's international obligations, that is not unprecedented and that domestic law should prevail.
The trouble is, the Northern Ireland Secretary Brandon Lewis has already said that overriding the Brexit divorce deal would break international law in a "very specific and limited way" and he's rejected Lord Keen's suggestion that he'd answered incorrectly.
That made it increasingly difficult for Lord Keen to reconcile the government's plans with the law and he wrote to the prime minister on Wednesday morning offering his resignation.
Lord Keen is the first member of the government to quit over its approach to this issue, which has drawn criticism from former attorneys general and all five living former prime ministers.
What is the Internal Markets Bill?
The bill sets out rules for the operation of the UK internal market - trade between England, Scotland, Wales and Northern Ireland - after the end of the Brexit transition period in January.
It proposes:
No new checks on goods moving from Northern Ireland to the rest of Great Britain Giving UK ministers powers to modify or "disapply" rules relating to the movement of goods that will come into force from 1 January if the UK and EU are unable to reach an alternative agreement through a trade deal Powers to override previously agreed obligations on state aid - government support for businessesThe bill explicitly states that these powers should apply even if they are incompatible with international law.
Ministers say the legislation is needed to prevent "damaging" tariffs on goods travelling from the rest of the UK to Northern Ireland if negotiations with the EU on a free trade agreement fail.
But some senior Conservatives - including former Prime Minister John Major - have warned it risks undermining the UK's reputation as an upholder of international law.
The legislation has also proved controversial with the devolved administrations, which are concerned about how the UK's "internal market" will operate post-Brexit and who will set regulations and standards.
Media playback is unsupported on your device
Meanwhile, European Commission President Ursula van der Leyen has warned the UK cannot unilaterally set aside the withdrawal agreement.
She said on Wednesday the agreement was the only way to guarantee the Northern Ireland peace process, and warned that its replacement would undermine trust in the UK.
Last week marked the start of the eighth round of post-Brexit trade deal talks between the UK and the EU.
The two sides are trying to secure a deal before the end of the transition period on 31 December, which will see the UK going onto generic World Trade Organisation rules if no agreement is reached.