The Prime Minister was left red-faced after he voted against his own State budget – by accidentally pressing the wrong button.
Rajoy was the only dissenting voice for his own budget when he pushed the red ‘no' button instead of the green when voting to pass an amendment put forward by the regional party Nueva Canarias, earning a round of applause from political opponents.
But luckily for him, his vote was not decisive and the budget was passed.
The move gives Rajoy's government considerable breathing space until 2019 as even if he fails to get agreement on next year's budget the constitution allows him to simply renew the previous spending plan.
Rajoy's Popular Party (PP) has only 137 of the 350 seats in parliament, far short of a majority, but the centre-right Ciudadanos and several regional parties also backed the budget in exchange for various concessions.
- Spanish PM will take stand in corruption trial
- Spain's socialist Pedro Sanchez just made a stunning comeback
- Spanish PM accuses Catalan separatists of blackmail
Ciudadanos, for instance, managed to incorporate some four billions ($4.5 billion) in social spending in the budget, while the PNV Basque nationalist party demanded more rail investment and a tax reform in its
Rajoy also promised more than a billion euros in investment over three years and tax benefits to lawmakers in the Canary Islands - one of whom only confirmed this weekend he would support the budget, giving Rajoy his much-needed majority.
The budget does not contain the spending cuts that marked years of crisis in Spain.
It plans for an increase in tax revenues compared to 2016 thanks in part to a rise in corporate tax, although business groups counter this may be too optimistic.
The text is now due to be validated mid-June by the upper house Senate, where Rajoy's PP has a majority, after which it will finally be adopted - eight months later than usual.
Spain went through a difficult political year in 2016 as two inconclusive general elections left the country without a fully-functioning government for 10 months.
But at the end of October, Rajoy, whose PP came first in both elections but without a majority, took power again thanks to his rival Socialists who abstained in the obligatory parliamentary vote that sees a candidate through to the premiership.
But he is now ruling at the head of a minority government, a far cry from his first election victory in 2011 when the PP got an absolute majority.
Meanwhile the Socialists have re-elected their former ousted chief Pedro Sanchez as leader and he has promised to make things difficult for the PP.