MISSOULA, Mont. (CN) — The city of Missoula sued the Montana attorney general this week, challenging his opinion that its ordinance requiring background checks on people who buy guns is unconstitutional.

Missoula, the state’s second-largest city after Billings, enacted a law in 2016 that required background checks on people who buy guns from private individuals within city limits. In enacting the ordinance, the city said it was closing a loophole in federal law that would allow felons to buy guns from private parties or unregistered firearms dealers without a background check.

Attorney General Tim Fox declared Missoula’s background check law unlawful in January 2017. The city sued him Wednesday in Missoula County Court, saying Fox’s ruling was incorrect.

Scott Stearns with Boone Karlberg filed the city’s lawsuit, which seeks declaratory judgment so that Missoula can enforce the background checks on private gun purchases within city limits. Everytown for Gun Safety is also a plaintiff.

Nineteen states have enacted laws that “close the loophole” on firearms purchases in private sales, according to the 11-page complaint.

Background checks on people buying guns through licensed dealers such as sporting goods stores are done through the National Instant Criminal Background Check System, known as NICS. Two exceptions to the city ordinance include transfers among family members or collectors.

In firearms transactions between private individuals, however, that system is not available. In 2016 the federal Bureau of Alcohol, Tobacco and Firearms issued guidelines and encouraged private sales to go through background checks by using a licensed dealer as intermediary. According to the BATF, several states have laws that prohibit the transfer of firearms between private individuals unless a NICS check is conducted on the person buying the gun.

Gary Marbut, a longtime proponent of gun-safety education and president of the Montana Shooting Sports Foundation, said he opposes the city’s lawsuit.

“It is a waste of taxpayer money, doomed, frivolous, anti-gun, illegal, unconstitutional, unenforceable, and poorly conceived public policy,” he said.

“Not only is this ordinance a misguided attempt at public policy, but it is being fraudulently sold to the public as a gun-safety measure.”

The complaint alleges that under Montana’s constitution a local government may exercise any power not prohibited by the constitution, law or charter.

But Fox’s January 2017 ruling expressly addressed how Missoula’s ordinance violates the Montana Constitution, which prohibits cities from exercising “any power that applies to or affects the right to keep or bear arms.”

The lawsuit alleges that Missoula’s gun ordinance was enacted lawfully, since it was intended to “promote public safety by preventing and suppressing the possession of firearms by convicted felons, adjudicated mental incompetents, illegal aliens and minors.”

The city says it is not subject to the Montana code concerning the right to keep and bear arms because the statute does not “specifically and expressly apply to cities with self-governing powers.”

Missoula says its background check ordinance prevents possession of guns by people already prohibited by law from possessing firearms. “It does not apply to or affect the right to keep or bear arms, much like the federal background check requirement does not implicate the right to keep and bear arms guaranteed in the Second Amendment of the U.S. Constitution,” the city says.

Montana law denies certain powers to local governments with self-governing powers, and a charter form of government, such as Missoula’s, does have self-governing powers, Fox wrote in his opinion.

“The right to keep arms … necessarily involves the right to purchase them,” Fox wrote, quoting Andrews v. State, 50 Tenn.

Missoula in 1987 tried to restrict the discharge of firearms within 5 miles of its city limits, and a Montana attorney general opinion at that time said the city exceeded its jurisdiction by passing a law that applied to activities outside its borders.

“Yet the city of Missoula expanded [its law] to regulate gun sales to felons, people adjudicated mentally ill, minors and illegal aliens,” Fox wrote in his 2017 opinion. “This dragnet approach on all gun sales or transfers ignores the long-standing statutory prohibitions.”

Fox also said that Montana does not report people who are adjudicated as mentally ill, because mental health records are confidential, and no exception in Montana law allows reporting of these records to the federal government.

Fox added that the term “illegal aliens” used in Missoula’s gun-sale ordinance has no legal bearing. He cited a 2016 Montana Supreme Court immigration case in which the high court struck down a law denying state services to people defined as “illegal aliens,” as that term is unknown in federal law, and it is unconstitutional to place federal immigration status into the hands of state officials.