Hi all! Faced with such a problem as phantom entries in the database. To check if I have gone crazy I created a separate page. This post is going to be long, so get ready. What do we have? We have some text (a book, a publication, a contract, etc.) we’ll use the term “article” as a base and store a list of paragraphs.
As you can see from the screenshot, we have 8 paragraphs (let’s remember that). During the editing of this article, the number of paragraphs may change. The functionality of our service implies deleting, editing, and adding new paragraphs with automatic numbering
and paragraph recalculation. During the work it was discovered that some articles did not have the same numbering of paragraphs as their actual number (and sequence). That is, an article of 5 paragraphs could be numbered as follows: 1, 2, 3, 5, 7. Below is a screenshot of a real article with this problem (test page).
Which, of course, is wrong, and I realized that the error is in the work of the process that recalculates these very paragraphs. I created an additional custom process, which is a loop, that recalculated all paragraphs at the press of a button and overwrote the information about the sequence number.
We created a test article with random paragraphs and tested this workflow. The process worked correctly, so I decided to use it to process our problem article. And it didn’t work.

After refreshing the page, everything was back to incorrect. I decided to watch it all through the debug mod and was impressed, upon inspection it turned out that everything worked correctly. But the data in the debugger was different from what was actually happening.

A workflow was created to create a new paragraph. Which simply takes the number of paragraphs from our article and adds + 1 to it.
Such a simple action, but again strange behavior. When you click on the button we add a paragraph with the correct sequence number, but after a moment it spontaneously changes (as if pulling information from a database). I assure you I don’t have any automatic processes in the backend to interact with paragraphs. I want to remind that we have only 8 paragraphs linked to the article in the database (first screenshot).

Then I began to think that perhaps the error in the frontend, and created a loop similar to the first but in the backend.
After checking, nothing changed. I decided to check and change everything manually

The magic of phantom records is back in action. Somehow, miraculously, the server part sees 10 records. I wondered what those entries were, so I set up saving information about them (I also messed up the numbering on purpose for clarity).

I would like to point out that the order of the phantom entries in this article is always identical. That is, the 9th and 5th paragraphs are phantom. If I delete the 2nd one, then of course the offset will be -1.
Then I wanted to try to re-link the entries in the hope that they will be miraculously “lost” in the custom state. But it didn’t happen. When I saved the paragraphs in the custom state, I cleaned up the links and relinked them again, everything stayed the same.
At this point, I don’t know how this bug happens. But I know how to fight it. I have to personally go into the database, delete the link of the first paragraph, save the changes, and then manually link this paragraph to the article again. Then the numbering will normalize.