MONTGOMERY, Ala. — Democrat Doug Jones scored a shocking upset in the Alabama Senate race Tuesday, capturing a Senate seat for Democrats in one of the reddest states in the nation.

Democrats had not even fielded a candidate for the seat when then-Sen. Jeff Sessions ran for re-election three years ago.

Jones's margin of victory was razor-thin with only a few thousand votes in the low-turnout special election.

Jones’ victory is a huge blow to the Republican Party and President Trump, who had weighed in heavily in favor of firebrand Republican candidate Roy Moore, despite allegations that he sexually harassed or pursued relationships with teenage girls when he was in his 30s.

Republicans began the year holding a thin 52-48 majority in the Senate and they have struggled to generate enough support in their own party to pass key legislative priorities like repealing the Affordable Care Act. With Jones taking a Republican seat, the majority is now 51 and Senate Majority Leader Mitch McConnell’s task became harder.

Black celebrities like former NBA star Charles Barkley and director Spike Lee weighed in on the Democratic side to try to spark enough black turnout to make a difference for Democrat Doug Jones, who prosecuted the Ku Klux Klan for the Birmingham church bombing in 1963 that killed four black girls.

The outcome of the race has national implications that could set the tone for the 2018 midterm elections:Jones is the first Democrat to represent Alabama in the Senate in 20 years It would also signal trouble ahead for the GOP in the 2018 midterms.

Jones's victory spares Senate Republicans having to wrestle with what to do with a freshman senator who is accused of sexual misconduct at a time when members of Congress are facing intense scrutiny over the issue and resigning their offices when accused.

The two campaigns, the result of Jeff Sessions leaving his Senate seat after 20 years to become President Trump's attorney general, have pursued different strategies. Jones needed to build a coalition of base Democratic voters and suburban Republicans, particularly Republican women who in the past have been unenthusiastic about Moore.

Jones hit his polling location near Birmingham early Tuesday, smiling for the cameras and onlooking voters.

"This is an important time in Alabama's history, and we feel very confident where we are and how this is going to turn out," he said.

Moore, as is his tradition, arrived to vote on horseback.

In Prattville, inn the center of the state, Mary Adair said she voted for Jones because she likes his personality and he talks about things that interest her.

"I feel like this: Judge Roy Moore, he’s been ousted from office twice already," she said. "We don’t need anybody else to get into the Senate and get ousted."

Guy Bulger voted in Prattville for Moore because Moore is a Republican and Bulger has voted that way since Ronald Reagan.

“I come from an old school,” he said. “And I remember one time they threw him out of the Capitol because he put the Ten Commandments in the capital of Alabama. And I appreciate that; I believe they needed to be (in state judicial building).”

Henrietta Boggs-MacGuire, 99, said she was surprised to see the long lines at her polling site at Huntingdon College in Montgomery.

“It’s surprisingly long because usually there are not that many," said Boggs-MacGuire, after casting her vote for Jones. “We have received so much attention, national attention. It means that people are trying to turn out and be good citizens and show off.’’

Jones has stressed a platform of job creation and health care while soft-pedaling other issues.

Moore has talked more about Donald Trump but otherwise has run a campaign not unlike his previous statewide ones, with an emphasis on getting his loyal base of voters to the polls. In his public appearances, he has stressed returning religion to the public sphere and attacked lesbian, gay, bisexual and transgender rights with a particular emphasis in this campaign on transgender individuals.

Moore repeatedly highlighted Trump’s decision to ban transgender people from the military. Yet the Pentagon confirmed Monday that it will begin accepting transgender troops Jan. 1, complying with a federal court order that overrules Trump's pledge.

The race has proven far closer than most recent Alabama campaigns, and Democrats spent this past weekend in a wide-ranging get-out-the-vote push through the state. The Jones campaign said in an email Monday that volunteers knocked on 80,000 doors during the weekend.

Moore made no public appearances between last Tuesday and Monday evening — he said he and Kayla Moore visited West Point, N.Y. — and has relied on surrogates and conservative media to make his case for him, including ex-White House strategist Steve Bannon.

The election results will partly belong to President Trump, who initially endorsed Moore’s primary opponent – the appointed Sen. Luther Strange – but forcefully backed Moore in the last week.

In a Tuesday tweet, Trump slammed Jones as a “puppet” for Democratic leaders and said Moore “will always vote with us.”

Trump also recorded a robocall for Moore and called on Alabamians to “get out and vote for Roy Moore” during a Friday rally across the Alabama border in in Pensacola, Fla.

Moore was twice removed from his position as chief justice of the state Supreme Court, once for refusing to remove a marble monument of the Ten Commandments from a state building, and later for refusing to enforce the state’s ban on same-sex marriage after it was deemed unconstitutional.