#109847UniqueConstraintViolationException when pasting copied records with category relations
Description
What happens
In TYPO3 12, 13 and 14 (v14.3.1) backend when I paste a copied part of a page tree that contains records (i.e. tt_content) which have categories assigned that reside within the same copied page tree part, I get the error message An exception occurred while executing a query: Duplicate entry 'xxx-xxx-tt_content-categories' for key 'PRIMARY'. xxx stands for the corresponding tt_content and sys_category UIDs. See the complete stack trace in txt format attached.
When you refresh the page tree after the error occured you see that page, content record and category were actually copied, but in somewhat unfinished fashion. I would expect to see the copied content record to be associated with the copied category and the original content record to be assigned the original category. Instead the copied content record and the original content record have both the original and the copied category assigned:
!{width: 50%;}copy-paste_06.png!
copy-paste_07.png
What I expected to happen
I expected the copied content record on the copied page to be assigned the copied category with no changes for the source records.
How to replicate
1. create a new page
2. create a new category on this page
3. create a content record on this page and assign it the category you just set up
!{width: 50%;}copy-paste_01.png!
copy-paste_02.png
4. copy the new page and paste it after the current page, i.e. itself
!{width: 50%;}copy-paste_03.png!
copy-paste_04.png
5. see the error message if your installation runs in debug mode
Additional observations
I initially encountered this error with news (tx_news_domain_model_news) records and categories in a TYPO3 v12 (12.4.x) installation and thought it may have to do with my specific setup. Before attempting to do any debugging I tried the simplified workflow described above in several different installations (including TYPO3 v13), both locally using ddev v1.25.2 and docker desktop v4.73.0 as well as on our servers. Eventually I set up a fresh default TYPO3 v14 installation (without any third party extensions) locally using the composer helper and ddev config only to be confronted with the same behaviour.
If I delete the primary key manually in sys_category_record_mm (using phpmyadmin) and afterwards copy and paste the page as described above I get the desired result: the page is copied and the copied content record on the copied page is assigned to the copied category only. Last but not least the original content record on the original page is assigned only the original category, not the copied one too.
But: with the primary key removed there are now three entries present in sys_category_record_mm regarding the copied content record and the copied category where I assume there should be only one.
Reviews 3
Files 8
History 34
-
Gerrit Code Review pushed patch set 1 for main to change 96966 status
NewUnder Review <span data-processor="relativetime" data-value="2026-10-21T09:10:11+00:00" > 10/21/26, 09:10 </span> -
Oliver Bartsch commented <span data-processor="relativetime" data-value="2026-10-21T09:10:54+00:00" > 10/21/26, 09:10 </span>
-
Oliver Bartsch changed Duplicates #79712 <span data-processor="relativetime" data-value="2026-10-23T08:20:44+00:00" > 10/23/26, 08:20 </span>
-
Oliver Bartsch commented and changed status
Under ReviewClosed <span data-processor="relativetime" data-value="2026-10-23T08:21:21+00:00" > 10/23/26, 08:21 </span>I’ll close this as duplicate of #79712
Hey,
thanks for the detailed report. Your test with the removed primary key helped a lot.
I can reproduce this. When a page is copied, both sides of the category relation are copied in the same run: the category (field
items) and the content element (fieldcategories). Copying the category first relates the new category to the original content element. Copying the content element then picks up both categories. Afterwards, DataHandler remaps all relations from the old uids to the new ones. After that mapping, two entries of the same field point to the same new record, andRelationHandler::writeMM()tries to insert that relation a second time. With the primary key in place this fails with the “Duplicate entry” error. Processing stops halfway, which is why the original content element keeps the copied category. Without the primary key, the extra rows get inserted, which is where your three duplicate rows come from.I pushed a patch for this.
DataHandler::remapListedDBRecords_procDBRefs()now removes entries that point to the same record after remapping an MM field that does not allow multiple relations.writeMM()then keeps one relation and deletes the stale ones, and the relation count on the copied record is updated too. After copying, the copied content element is related only to the copied category, and the original records are unchanged. The same applies to other bidirectional MM relations where both sides are copied together, for example news records and their categories. The patch adds a functional test that copies a page containing a category and a content element related to it.Cheers, Oli