Repository: paperless-ngx/paperless-ngx

Issue:
[BUG] Saving document fails if triggered workflow action adds custom field and field already exists
### Description

I created two new workflows to add either a boolean or date custom field to documents of the type of invoice.
To decide which of the custom fields i use a regular expression to look for a year of 2023 or before to just add the bool to be able to check off that I paid the invoice. When I detect a year of 2024 or later I add the date custom field to enter the exact date when i paid the invoice.
The workflows both have triggers for when a document is added and also when a document is edited.

When importing a document, the workflows correctly add the appropriate custom field. Editing details and trying to save them produces the error shown in the browser and the UI is in the state of not having saved. But there are no errors in the log and refreshing the document shows that the edits were saved correctly.
When editing the document to another document type so that the triggers no longer apply does not produce the error.
When changing the document back to the invoice document type so that the triggers apply once again does not produce the error at first, but after refreshing the document the error appears again after edits.

### Steps to reproduce

1. Create a workflow with triggers for document added and document edited
2. Add a document type filter in both triggers
3. Also add a content regex filter in both triggers
4. Add an action to add a custom field to the workflow
5. Import a new document or edit an existing document so that it has the appropriate document type and the regex filter matches so that the custom field is added to the document
6. Edit the title and hit save

### Webserver logs

```bash
no error in logs but in browser, see below
```


### Browser logs

```bash
{"headers":{"normalizedNames":{},"lazyUpdate":null},"status":500,"statusText":"OK","url":"https://paperless.domain.com/api/documents/69/","ok":false,"name":"HttpErrorResponse","message":"Http failure response for https://paperless.domain.com/api/documents/69/: 500 OK","error":"\n<!doctype html>\n<html lang=\"en\">\n<head>\n  <title>Server Error (500)</title>\n</head>\n<body>\n  <h1>Server Error (500)</h1><p></p>\n</body>\n</html>\n"}
```


### Paperless-ngx version

2.3.0 + 2.3.1

### Host OS

DSM 7.2.1-69057 Update 3

### Installation method

Docker - official image

### Browser

Safari

### Configuration changes

_No response_

### Other

_No response_

### Please confirm the following

- [X] I believe this issue is a bug that affects all users of Paperless-ngx, not something specific to my installation.
- [X] I have already searched for relevant existing issues and discussions before opening this report.
- [X] I have updated the title field above with a concise description.


Discussion:
![error](https://github.com/paperless-ngx/paperless-ngx/assets/79487102/a2072325-14bd-46e9-bb76-a9902906087a)

Please provide the paperless logs
```[2024-01-07 13:51:57,825] [DEBUG] [paperless.matching] DocumentType Rechnung matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen because it contains this string: "Rechnung"
[2024-01-07 13:52:04,146] [INFO] [paperless.matching] Document did not match Workflow: Alte Rechnung bezahlt
[2024-01-07 13:52:04,150] [DEBUG] [paperless.matching] ("Document content matching settings for algorithm '4' did not match",)
[2024-01-07 13:52:04,158] [DEBUG] [paperless.matching] WorkflowTrigger WorkflowTrigger 6 matched on document 2024-01-05 Achtung: Bestellung enthält abweichende Mengen because the string datum: 05.01.2024 matches the regular expression (?<!Geburts)datum\:\s[0-9]{2}\.[0-9]{2}\.(202[4-9]|20[3-9][0-9]|2[1-9][0-9][0-9])
[2024-01-07 13:52:04,163] [INFO] [paperless.matching] Document matched WorkflowTrigger 6 from Workflow: Rechnung bezahlt am
[2024-01-07 13:52:04,169] [INFO] [paperless.handlers] Applying WorkflowAction 3 from Workflow: Rechnung bezahlt am
[2024-01-07 13:52:11,004] [DEBUG] [paperless.matching] DocumentType Rechnung matched on document 2024-01-05 Achtung: Bestellung enthält abweichende Mengen because it contains this string: "Rechnung"
[2024-01-07 13:52:17,630] [INFO] [paperless.matching] Document did not match Workflow: Alte Rechnung bezahlt
[2024-01-07 13:52:17,632] [DEBUG] [paperless.matching] ("Document content matching settings for algorithm '4' did not match",)
[2024-01-07 13:52:17,639] [DEBUG] [paperless.matching] WorkflowTrigger WorkflowTrigger 6 matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen because the string datum: 05.01.2024 matches the regular expression (?<!Geburts)datum\:\s[0-9]{2}\.[0-9]{2}\.(202[4-9]|20[3-9][0-9]|2[1-9][0-9][0-9])
[2024-01-07 13:52:17,644] [INFO] [paperless.matching] Document matched WorkflowTrigger 6 from Workflow: Rechnung bezahlt am
[2024-01-07 13:52:17,648] [INFO] [paperless.handlers] Applying WorkflowAction 3 from Workflow: Rechnung bezahlt am
[2024-01-07 13:52:33,499] [DEBUG] [paperless.matching] DocumentType Rechnung matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen because it contains this string: "Rechnung"
[2024-01-07 13:52:52,315] [INFO] [paperless.matching] Document did not match Workflow: Alte Rechnung bezahlt
[2024-01-07 13:52:52,316] [DEBUG] [paperless.matching] ("Document content matching settings for algorithm '4' did not match",)
[2024-01-07 13:52:52,323] [DEBUG] [paperless.matching] WorkflowTrigger WorkflowTrigger 6 matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen because the string datum: 05.01.2024 matches the regular expression (?<!Geburts)datum\:\s[0-9]{2}\.[0-9]{2}\.(202[4-9]|20[3-9][0-9]|2[1-9][0-9][0-9])
[2024-01-07 13:52:52,327] [INFO] [paperless.matching] Document matched WorkflowTrigger 6 from Workflow: Rechnung bezahlt am
[2024-01-07 13:52:52,332] [INFO] [paperless.handlers] Applying WorkflowAction 3 from Workflow: Rechnung bezahlt am
[2024-01-07 13:58:18,577] [DEBUG] [paperless.matching] DocumentType Lieferschein matched on document 2023-12-07 Otto Belkin Dock because it contains all of these words: Lieferschein
[2024-01-07 13:58:27,206] [INFO] [paperless.matching] Document did not match Workflow: Alte Rechnung bezahlt
[2024-01-07 13:58:27,208] [DEBUG] [paperless.matching] ('Document doc type Lieferschein does not match Rechnung',)
[2024-01-07 13:58:27,219] [INFO] [paperless.matching] Document did not match Workflow: Rechnung bezahlt am
[2024-01-07 13:58:27,220] [DEBUG] [paperless.matching] ('Document doc type Lieferschein does not match Rechnung',)
[2024-01-07 13:58:32,734] [DEBUG] [paperless.matching] Correspondent DB Systel GmbH matched on document 2023-12-08 DB Systel GmbH QMF Sprintwechsel because it contains this string: "DB Systel GmbH"
[2024-01-07 13:58:32,789] [DEBUG] [paperless.matching] DocumentType Reisekostenabrechnung matched on document 2023-12-08 DB Systel GmbH QMF Sprintwechsel because it contains all of these words: Reisekostenabrechnung
[2024-01-07 13:58:34,084] [DEBUG] [paperless.matching] DocumentType Lieferschein matched on document 2023-12-07 Otto Belkin Dock because it contains all of these words: Lieferschein
[2024-01-07 13:58:50,513] [INFO] [paperless.matching] Document did not match Workflow: Alte Rechnung bezahlt
[2024-01-07 13:58:50,514] [DEBUG] [paperless.matching] ('Document doc type Lieferschein does not match Rechnung',)
[2024-01-07 13:58:50,524] [INFO] [paperless.matching] Document did not match Workflow: Rechnung bezahlt am
[2024-01-07 13:58:50,525] [DEBUG] [paperless.matching] ('Document doc type Lieferschein does not match Rechnung',)
[2024-01-07 13:59:09,606] [DEBUG] [paperless.matching] DocumentType Lieferschein matched on document 2023-12-07 Otto Belkin Dock because it contains all of these words: Lieferschein
[2024-01-07 13:59:14,153] [INFO] [paperless.matching] Document did not match Workflow: Alte Rechnung bezahlt
[2024-01-07 13:59:14,155] [DEBUG] [paperless.matching] ('Document doc type Lieferschein does not match Rechnung',)
[2024-01-07 13:59:14,165] [INFO] [paperless.matching] Document did not match Workflow: Rechnung bezahlt am
[2024-01-07 13:59:14,166] [DEBUG] [paperless.matching] ('Document doc type Lieferschein does not match Rechnung',)
[2024-01-07 13:59:19,942] [DEBUG] [paperless.matching] Correspondent DB Systel GmbH matched on document 2023-12-08 DB Systel GmbH QMF Sprintwechsel because it contains this string: "DB Systel GmbH"
[2024-01-07 13:59:19,982] [DEBUG] [paperless.matching] DocumentType Reisekostenabrechnung matched on document 2023-12-08 DB Systel GmbH QMF Sprintwechsel because it contains all of these words: Reisekostenabrechnung
[2024-01-07 13:59:30,604] [DEBUG] [paperless.matching] WorkflowTrigger WorkflowTrigger 4 matched on document 2023-12-08 DB Systel GmbH QMF Sprintwechsel because the string datum: 08.12.2023 matches the regular expression (?<!Geburts)datum\:\s[0-9]{2}\.[0-9]{2}\.(19[0-9][0-9]|20[0-1][0-9]|2023)
[2024-01-07 13:59:30,610] [INFO] [paperless.matching] Document did not match Workflow: Alte Rechnung bezahlt
[2024-01-07 13:59:30,611] [DEBUG] [paperless.matching] ('Document doc type Reisekostenabrechnung does not match Rechnung',)
[2024-01-07 13:59:30,622] [INFO] [paperless.matching] Document did not match Workflow: Rechnung bezahlt am
[2024-01-07 13:59:30,622] [DEBUG] [paperless.matching] ('Document doc type Reisekostenabrechnung does not match Rechnung',)
[2024-01-07 13:59:39,786] [DEBUG] [paperless.matching] Correspondent DB Systel GmbH matched on document 2023-12-08 DB Systel GmbH QMF Sprintwechsel because it contains this string: "DB Systel GmbH"
[2024-01-07 13:59:39,823] [DEBUG] [paperless.matching] DocumentType Reisekostenabrechnung matched on document 2023-12-08 DB Systel GmbH QMF Sprintwechsel because it contains all of these words: Reisekostenabrechnung
[2024-01-07 13:59:48,916] [DEBUG] [paperless.matching] WorkflowTrigger WorkflowTrigger 4 matched on document 2023-12-05 DB Systel GmbH QMF Sprintwechsel because the string datum: 08.12.2023 matches the regular expression (?<!Geburts)datum\:\s[0-9]{2}\.[0-9]{2}\.(19[0-9][0-9]|20[0-1][0-9]|2023)
[2024-01-07 13:59:48,921] [INFO] [paperless.matching] Document did not match Workflow: Alte Rechnung bezahlt
[2024-01-07 13:59:48,922] [DEBUG] [paperless.matching] ('Document doc type Reisekostenabrechnung does not match Rechnung',)
[2024-01-07 13:59:48,933] [INFO] [paperless.matching] Document did not match Workflow: Rechnung bezahlt am
[2024-01-07 13:59:48,934] [DEBUG] [paperless.matching] ('Document doc type Reisekostenabrechnung does not match Rechnung',)
[2024-01-07 13:59:52,509] [DEBUG] [paperless.matching] WorkflowTrigger WorkflowTrigger 4 matched on document 2023-12-08 DB Systel GmbH QMF Sprintwechsel because the string datum: 08.12.2023 matches the regular expression (?<!Geburts)datum\:\s[0-9]{2}\.[0-9]{2}\.(19[0-9][0-9]|20[0-1][0-9]|2023)
[2024-01-07 13:59:52,515] [INFO] [paperless.matching] Document did not match Workflow: Alte Rechnung bezahlt
[2024-01-07 13:59:52,515] [DEBUG] [paperless.matching] ('Document doc type Reisekostenabrechnung does not match Rechnung',)
[2024-01-07 13:59:52,526] [INFO] [paperless.matching] Document did not match Workflow: Rechnung bezahlt am
[2024-01-07 13:59:52,528] [DEBUG] [paperless.matching] ('Document doc type Reisekostenabrechnung does not match Rechnung',)
[2024-01-07 13:59:55,759] [DEBUG] [paperless.matching] WorkflowTrigger WorkflowTrigger 4 matched on document 2023-12-08 DB Systel GmbH QMF Sprintwechsel because the string datum: 08.12.2023 matches the regular expression (?<!Geburts)datum\:\s[0-9]{2}\.[0-9]{2}\.(19[0-9][0-9]|20[0-1][0-9]|2023)
[2024-01-07 13:59:55,764] [INFO] [paperless.matching] Document did not match Workflow: Alte Rechnung bezahlt
[2024-01-07 13:59:55,765] [DEBUG] [paperless.matching] ('Document doc type Reisekostenabrechnung does not match Rechnung',)
[2024-01-07 13:59:55,778] [INFO] [paperless.matching] Document did not match Workflow: Rechnung bezahlt am
[2024-01-07 13:59:55,779] [DEBUG] [paperless.matching] ('Document doc type Reisekostenabrechnung does not match Rechnung',)
[2024-01-07 14:04:44,106] [DEBUG] [paperless.matching] DocumentType Rechnung matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen because it contains this string: "Rechnung"
[2024-01-07 14:04:47,308] [INFO] [paperless.matching] Document did not match Workflow: Alte Rechnung bezahlt
[2024-01-07 14:04:47,308] [DEBUG] [paperless.matching] ("Document content matching settings for algorithm '4' did not match",)
[2024-01-07 14:04:47,324] [DEBUG] [paperless.matching] WorkflowTrigger WorkflowTrigger 6 matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen because the string datum: 05.01.2024 matches the regular expression (?<!Geburts)datum\:\s[0-9]{2}\.[0-9]{2}\.(202[4-9]|20[3-9][0-9]|2[1-9][0-9][0-9])
[2024-01-07 14:04:47,329] [INFO] [paperless.matching] Document matched WorkflowTrigger 6 from Workflow: Rechnung bezahlt am
[2024-01-07 14:04:47,334] [INFO] [paperless.handlers] Applying WorkflowAction 3 from Workflow: Rechnung bezahlt am
[2024-01-07 14:04:54,717] [DEBUG] [paperless.matching] DocumentType Rechnung matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen because it contains this string: "Rechnung"
[2024-01-07 14:05:00,023] [INFO] [paperless.matching] Document did not match Workflow: Alte Rechnung bezahlt
[2024-01-07 14:05:00,026] [DEBUG] [paperless.matching] ("Document content matching settings for algorithm '4' did not match",)
[2024-01-07 14:05:00,032] [DEBUG] [paperless.matching] WorkflowTrigger WorkflowTrigger 6 matched on document 2024-01-05 Achtung: Bestellung enthält abweichende Mengen because the string datum: 05.01.2024 matches the regular expression (?<!Geburts)datum\:\s[0-9]{2}\.[0-9]{2}\.(202[4-9]|20[3-9][0-9]|2[1-9][0-9][0-9])
[2024-01-07 14:05:00,037] [INFO] [paperless.matching] Document matched WorkflowTrigger 6 from Workflow: Rechnung bezahlt am
[2024-01-07 14:05:00,046] [INFO] [paperless.handlers] Applying WorkflowAction 3 from Workflow: Rechnung bezahlt am
[2024-01-07 14:05:02,299] [DEBUG] [paperless.classifier] Gathering data from database...
[2024-01-07 14:05:02,728] [DEBUG] [paperless.classifier] 63 documents, 2 tag(s), 0 correspondent(s), 1 document type(s). 0 storage path(es)
[2024-01-07 14:05:02,729] [DEBUG] [paperless.classifier] Vectorizing data...
[2024-01-07 14:05:04,385] [DEBUG] [paperless.classifier] Training tags classifier...
[2024-01-07 14:05:07,736] [DEBUG] [paperless.classifier] There are no correspondents. Not training correspondent classifier.
[2024-01-07 14:05:07,737] [DEBUG] [paperless.classifier] Training document type classifier...
[2024-01-07 14:05:09,913] [INFO] [paperless.matching] Document did not match Workflow: Alte Rechnung bezahlt
[2024-01-07 14:05:09,922] [DEBUG] [paperless.matching] ("Document content matching settings for algorithm '4' did not match",)
[2024-01-07 14:05:09,936] [DEBUG] [paperless.matching] WorkflowTrigger WorkflowTrigger 6 matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen because the string datum: 05.01.2024 matches the regular expression (?<!Geburts)datum\:\s[0-9]{2}\.[0-9]{2}\.(202[4-9]|20[3-9][0-9]|2[1-9][0-9][0-9])
[2024-01-07 14:05:10,023] [INFO] [paperless.matching] Document matched WorkflowTrigger 6 from Workflow: Rechnung bezahlt am
[2024-01-07 14:05:10,027] [INFO] [paperless.handlers] Applying WorkflowAction 3 from Workflow: Rechnung bezahlt am
[2024-01-07 14:05:11,523] [DEBUG] [paperless.classifier] There are no storage paths. Not training storage path classifier.
[2024-01-07 14:05:11,524] [INFO] [paperless.tasks] Saving updated classifier model to /usr/src/paperless/data/classification_model.pickle...
[2024-01-07 14:05:13,742] [DEBUG] [paperless.matching] DocumentType Rechnung matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen because it contains this string: "Rechnung"
[2024-01-07 14:15:38,336] [DEBUG] [paperless.matching] DocumentType Rechnung matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen because it contains this string: "Rechnung"
[2024-01-07 14:15:45,150] [INFO] [paperless.matching] Document did not match Workflow: Alte Rechnung bezahlt
[2024-01-07 14:15:45,150] [DEBUG] [paperless.matching] ('Document doc type Mitteilung does not match Rechnung',)
[2024-01-07 14:15:45,157] [DEBUG] [paperless.matching] WorkflowTrigger WorkflowTrigger 6 matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen because the string datum: 05.01.2024 matches the regular expression (?<!Geburts)datum\:\s[0-9]{2}\.[0-9]{2}\.(202[4-9]|20[3-9][0-9]|2[1-9][0-9][0-9])
[2024-01-07 14:15:45,162] [INFO] [paperless.matching] Document did not match Workflow: Rechnung bezahlt am
[2024-01-07 14:15:45,163] [DEBUG] [paperless.matching] ('Document doc type Mitteilung does not match Rechnung',)
[2024-01-07 14:15:52,236] [INFO] [paperless.matching] Document did not match Workflow: Alte Rechnung bezahlt
[2024-01-07 14:15:52,237] [DEBUG] [paperless.matching] ("Document content matching settings for algorithm '4' did not match",)
[2024-01-07 14:15:52,243] [DEBUG] [paperless.matching] WorkflowTrigger WorkflowTrigger 6 matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen because the string datum: 05.01.2024 matches the regular expression (?<!Geburts)datum\:\s[0-9]{2}\.[0-9]{2}\.(202[4-9]|20[3-9][0-9]|2[1-9][0-9][0-9])
[2024-01-07 14:15:52,248] [INFO] [paperless.matching] Document matched WorkflowTrigger 6 from Workflow: Rechnung bezahlt am
[2024-01-07 14:15:52,253] [INFO] [paperless.handlers] Applying WorkflowAction 3 from Workflow: Rechnung bezahlt am
[2024-01-07 14:15:55,315] [DEBUG] [paperless.matching] DocumentType Rechnung matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen because it contains this string: "Rechnung"
[2024-01-07 14:16:01,779] [INFO] [paperless.matching] Document did not match Workflow: Alte Rechnung bezahlt
[2024-01-07 14:16:01,779] [DEBUG] [paperless.matching] ('Document doc type Mitteilung does not match Rechnung',)
[2024-01-07 14:16:01,785] [DEBUG] [paperless.matching] WorkflowTrigger WorkflowTrigger 6 matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen because the string datum: 05.01.2024 matches the regular expression (?<!Geburts)datum\:\s[0-9]{2}\.[0-9]{2}\.(202[4-9]|20[3-9][0-9]|2[1-9][0-9][0-9])
[2024-01-07 14:16:01,790] [INFO] [paperless.matching] Document did not match Workflow: Rechnung bezahlt am
[2024-01-07 14:16:01,791] [DEBUG] [paperless.matching] ('Document doc type Mitteilung does not match Rechnung',)
[2024-01-07 14:16:05,086] [DEBUG] [paperless.matching] DocumentType Rechnung matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen because it contains this string: "Rechnung"
[2024-01-07 14:16:10,397] [INFO] [paperless.matching] Document did not match Workflow: Alte Rechnung bezahlt
[2024-01-07 14:16:10,397] [DEBUG] [paperless.matching] ("Document content matching settings for algorithm '4' did not match",)
[2024-01-07 14:16:10,405] [DEBUG] [paperless.matching] WorkflowTrigger WorkflowTrigger 6 matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen because the string datum: 05.01.2024 matches the regular expression (?<!Geburts)datum\:\s[0-9]{2}\.[0-9]{2}\.(202[4-9]|20[3-9][0-9]|2[1-9][0-9][0-9])
[2024-01-07 14:16:10,410] [INFO] [paperless.matching] Document matched WorkflowTrigger 6 from Workflow: Rechnung bezahlt am
[2024-01-07 14:16:10,415] [INFO] [paperless.handlers] Applying WorkflowAction 3 from Workflow: Rechnung bezahlt am
[2024-01-07 14:16:12,106] [DEBUG] [paperless.matching] DocumentType Rechnung matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen because it contains this string: "Rechnung"
[2024-01-07 14:16:18,560] [INFO] [paperless.matching] Document did not match Workflow: Alte Rechnung bezahlt
[2024-01-07 14:16:18,561] [DEBUG] [paperless.matching] ("Document content matching settings for algorithm '4' did not match",)
[2024-01-07 14:16:18,567] [DEBUG] [paperless.matching] WorkflowTrigger WorkflowTrigger 6 matched on document 2024-01-06 Beitragsservice Achtung: Bestellung enthält abweichende Mengen because the string datum: 05.01.2024 matches the regular expression (?<!Geburts)datum\:\s[0-9]{2}\.[0-9]{2}\.(202[4-9]|20[3-9][0-9]|2[1-9][0-9][0-9])
[2024-01-07 14:16:18,572] [INFO] [paperless.matching] Document matched WorkflowTrigger 6 from Workflow: Rechnung bezahlt am
[2024-01-07 14:16:18,576] [INFO] [paperless.handlers] Applying WorkflowAction 3 from Workflow: Rechnung bezahlt am
[2024-01-07 14:16:22,033] [DEBUG] [paperless.matching] DocumentType Rechnung matched on document 2024-01-06 Beitragsservice Achtung: Bestellung enthält abweichende Mengen because it contains this string: "Rechnung"
[2024-01-07 14:16:30,324] [INFO] [paperless.matching] Document did not match Workflow: Alte Rechnung bezahlt
[2024-01-07 14:16:30,325] [DEBUG] [paperless.matching] ("Document content matching settings for algorithm '4' did not match",)
[2024-01-07 14:16:30,332] [DEBUG] [paperless.matching] WorkflowTrigger WorkflowTrigger 6 matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen because the string datum: 05.01.2024 matches the regular expression (?<!Geburts)datum\:\s[0-9]{2}\.[0-9]{2}\.(202[4-9]|20[3-9][0-9]|2[1-9][0-9][0-9])
[2024-01-07 14:16:30,336] [INFO] [paperless.matching] Document matched WorkflowTrigger 6 from Workflow: Rechnung bezahlt am
[2024-01-07 14:16:30,340] [INFO] [paperless.handlers] Applying WorkflowAction 3 from Workflow: Rechnung bezahlt am
[2024-01-07 14:16:40,378] [INFO] [paperless.matching] Document did not match Workflow: Alte Rechnung bezahlt
[2024-01-07 14:16:40,380] [DEBUG] [paperless.matching] ('Document doc type Mitteilung does not match Rechnung',)
[2024-01-07 14:16:40,386] [DEBUG] [paperless.matching] WorkflowTrigger WorkflowTrigger 6 matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen because the string datum: 05.01.2024 matches the regular expression (?<!Geburts)datum\:\s[0-9]{2}\.[0-9]{2}\.(202[4-9]|20[3-9][0-9]|2[1-9][0-9][0-9])
[2024-01-07 14:16:40,391] [INFO] [paperless.matching] Document did not match Workflow: Rechnung bezahlt am
[2024-01-07 14:16:40,392] [DEBUG] [paperless.matching] ('Document doc type Mitteilung does not match Rechnung',)
[2024-01-07 14:16:44,716] [INFO] [paperless.matching] Document did not match Workflow: Alte Rechnung bezahlt
[2024-01-07 14:16:44,718] [DEBUG] [paperless.matching] ('Document doc type Mitteilung does not match Rechnung',)
[2024-01-07 14:16:44,728] [DEBUG] [paperless.matching] WorkflowTrigger WorkflowTrigger 6 matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen because the string datum: 05.01.2024 matches the regular expression (?<!Geburts)datum\:\s[0-9]{2}\.[0-9]{2}\.(202[4-9]|20[3-9][0-9]|2[1-9][0-9][0-9])
[2024-01-07 14:16:44,734] [INFO] [paperless.matching] Document did not match Workflow: Rechnung bezahlt am
[2024-01-07 14:16:44,735] [DEBUG] [paperless.matching] ('Document doc type Mitteilung does not match Rechnung',)
[2024-01-07 14:29:42,689] [DEBUG] [paperless.matching] DocumentType Rechnung matched on document 2024-01-03 Fwd: Vielen Dank für Ihren Einkauf bei Steam! because it contains this string: "Rechnung"
[2024-01-07 14:39:32,864] [DEBUG] [paperless.matching] DocumentType Rechnung matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen because it contains this string: "Rechnung"
[2024-01-07 14:39:36,363] [INFO] [paperless.matching] Document did not match Workflow: Alte Rechnung bezahlt
[2024-01-07 14:39:36,365] [DEBUG] [paperless.matching] ("Document content matching settings for algorithm '4' did not match",)
[2024-01-07 14:39:36,372] [DEBUG] [paperless.matching] WorkflowTrigger WorkflowTrigger 6 matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen because the string datum: 05.01.2024 matches the regular expression (?<!Geburts)datum\:\s[0-9]{2}\.[0-9]{2}\.(202[4-9]|20[3-9][0-9]|2[1-9][0-9][0-9])
[2024-01-07 14:39:36,377] [INFO] [paperless.matching] Document matched WorkflowTrigger 6 from Workflow: Rechnung bezahlt am
[2024-01-07 14:39:36,382] [INFO] [paperless.handlers] Applying WorkflowAction 3 from Workflow: Rechnung bezahlt am
[2024-01-07 14:39:40,139] [DEBUG] [paperless.matching] DocumentType Rechnung matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen because it contains this string: "Rechnung"
[2024-01-07 14:40:16,133] [INFO] [paperless.matching] Document did not match Workflow: Alte Rechnung bezahlt
[2024-01-07 14:40:16,134] [DEBUG] [paperless.matching] ("Document content matching settings for algorithm '4' did not match",)
[2024-01-07 14:40:16,140] [DEBUG] [paperless.matching] WorkflowTrigger WorkflowTrigger 6 matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen Test because the string datum: 05.01.2024 matches the regular expression (?<!Geburts)datum\:\s[0-9]{2}\.[0-9]{2}\.(202[4-9]|20[3-9][0-9]|2[1-9][0-9][0-9])
[2024-01-07 14:40:16,146] [INFO] [paperless.matching] Document matched WorkflowTrigger 6 from Workflow: Rechnung bezahlt am
[2024-01-07 14:40:16,150] [INFO] [paperless.handlers] Applying WorkflowAction 3 from Workflow: Rechnung bezahlt am
[2024-01-07 14:40:39,614] [DEBUG] [paperless.matching] DocumentType Rechnung matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen Test because it contains this string: "Rechnung"
[2024-01-07 14:40:57,153] [DEBUG] [paperless.matching] DocumentType Rechnung matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Mengen Test because it contains this string: "Rechnung"
[2024-01-07 14:41:12,463] [INFO] [paperless.matching] Document did not match Workflow: Alte Rechnung bezahlt
[2024-01-07 14:41:12,464] [DEBUG] [paperless.matching] ("Document content matching settings for algorithm '4' did not match",)
[2024-01-07 14:41:12,471] [DEBUG] [paperless.matching] WorkflowTrigger WorkflowTrigger 6 matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Menge because the string datum: 05.01.2024 matches the regular expression (?<!Geburts)datum\:\s[0-9]{2}\.[0-9]{2}\.(202[4-9]|20[3-9][0-9]|2[1-9][0-9][0-9])
[2024-01-07 14:41:12,476] [INFO] [paperless.matching] Document matched WorkflowTrigger 6 from Workflow: Rechnung bezahlt am
[2024-01-07 14:41:12,480] [INFO] [paperless.handlers] Applying WorkflowAction 3 from Workflow: Rechnung bezahlt am
[2024-01-07 14:41:37,968] [DEBUG] [paperless.matching] DocumentType Rechnung matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Menge because it contains this string: "Rechnung"
[2024-01-07 14:41:39,130] [DEBUG] [paperless.matching] DocumentType Rechnung matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Menge because it contains this string: "Rechnung"
[2024-01-07 14:41:47,222] [INFO] [paperless.matching] Document did not match Workflow: Alte Rechnung bezahlt
[2024-01-07 14:41:47,223] [DEBUG] [paperless.matching] ("Document content matching settings for algorithm '4' did not match",)
[2024-01-07 14:41:47,229] [DEBUG] [paperless.matching] WorkflowTrigger WorkflowTrigger 6 matched on document 2024-01-06 Achtung: Bestellung enthält abweichende Menge Test because the string datum: 05.01.2024 matches the regular expression (?<!Geburts)datum\:\s[0-9]{2}\.[0-9]{2}\.(202[4-9]|20[3-9][0-9]|2[1-9][0-9][0-9])
[2024-01-07 14:41:47,235] [INFO] [paperless.matching] Document matched WorkflowTrigger 6 from Workflow: Rechnung bezahlt am
[2024-01-07 14:41:47,239] [INFO] [paperless.handlers] Applying WorkflowAction 3 from Workflow: Rechnung bezahlt am
[2024-01-07 15:05:02,251] [DEBUG] [paperless.classifier] Gathering data from database...
[2024-01-07 15:05:02,660] [DEBUG] [paperless.tasks] Training data unchanged.
[2024-01-07 16:05:01,909] [DEBUG] [paperless.classifier] Gathering data from database...
[2024-01-07 16:05:02,331] [DEBUG] [paperless.tasks] Training data unchanged.
[2024-01-07 17:05:01,908] [DEBUG] [paperless.classifier] Gathering data from database...
[2024-01-07 17:05:02,325] [DEBUG] [paperless.tasks] Training data unchanged.
[2024-01-07 18:05:02,048] [DEBUG] [paperless.classifier] Gathering data from database...
[2024-01-07 18:05:02,459] [DEBUG] [paperless.tasks] Training data unchanged.```


This is the log from where i tried around and produced the error in the UI a few times.
Nothing in the logs indicates to me that the system experiences a problem, yet the UI tells me above.
Can you copy & paste the request "payload" (in browser dev tools look under network tab). Of course you can remove any private info
i am having trouble identifying the payload information.
this is what i am seeing under the network tab after the error showed on screen:
![Bild 07 01 24 um 18 27](https://github.com/paperless-ngx/paperless-ngx/assets/79487102/7765a31b-9163-4a2b-958f-c009d37aee63)


Should be under a request to `/api/documents/ID`, something like:

<img width="837" alt="Screenshot 2024-01-07 at 9 30 26 AM" src="https://github.com/paperless-ngx/paperless-ngx/assets/4887959/0bc17f57-f5f4-4aba-85b8-aa7b56ed01d8">

After you hit the 'save' button
stupid apple making it extra hard to get that info:
![Bild 07 01 24 um 18 34](https://github.com/paperless-ngx/paperless-ngx/assets/79487102/06b6fe49-d6b5-4b56-8ba0-c49a400c2467)
i need to press that tiny little arrow on the bottom there to get to the payload lol

```
{
  "id": 69,
  "correspondent": null,
  "document_type": 8,
  "storage_path": null,
  "title": "Achtung: Bestellung enthält abweichende Menge Test",
  "content": "redacted",
  "tags": [
    1
  ],
  "created_date": "2024-01-05",
  "modified": "2024-01-07T14:41:46.945475+01:00",
  "added": "2024-01-06T09:00:48.738886+01:00",
  "archive_serial_number": null,
  "original_file_name": "Avis_2024_01_06_659421586.pdf",
  "archived_file_name": "2024-01-06 Achtung- Bestellung enthält abweichende Menge Test.pdf",
  "owner": 3,
  "permissions": {
    "view": {
      "users": [],
      "groups": []
    },
    "change": {
      "users": [],
      "groups": []
    }
  },
  "notes": [],
  "custom_fields": [
    {
      "field": 4,
      "value": null
    }
  ],
  "set_permissions": {
    "view": {
      "users": [],
      "groups": []
    },
    "change": {
      "users": [],
      "groups": []
    }
  }
}
```
Is the field with ID#4 (the one in the doc you're saving in your last comment) a bool field?
it is a date field in this document, but ive seen it happen with a bool field as well

i can save the document fine if i change the document type so that the workflow no longer applies
Sorry for all the questions, this is a very specific setup, can you send screenshot(s) of the relevant workflows?
of course i understand, thank you for working with me on this!
screenshots in a bit
Workflow:
![Bild 07 01 24 um 19 00](https://github.com/paperless-ngx/paperless-ngx/assets/79487102/5400baee-bc37-469a-ad45-3a9f5a501961)
![Bild 07 01 24 um 19 00 (1)](https://github.com/paperless-ngx/paperless-ngx/assets/79487102/6b6cf9a9-ba6e-4c8c-815a-733cfd8d7cb4)

Regex: `(?<!Geburts)datum\:\s[0-9]{2}\.[0-9]{2}\.(202[4-9]|20[3-9][0-9]|2[1-9][0-9][0-9])`

Document Type:
![Bild 07 01 24 um 19 01](https://github.com/paperless-ngx/paperless-ngx/assets/79487102/5600677f-ab4f-4aea-864d-eb3454cd0a2a)

Test document:
[bug-test-workflows.pdf](https://github.com/paperless-ngx/paperless-ngx/files/13854420/bug-test-workflows.pdf)

Thanks, got it, I'll look into it

```
{"headers":{"normalizedNames":{},"lazyUpdate":null},"status":500,"statusText":"Internal Server Error","url":"http://localhost:8000/api/documents/377/","ok":false,"name":"HttpErrorResponse","message":"Http failure response for http://localhost:8000/api/documents/377/: 500 Internal Server Error","error":"IntegrityError at /api/documents/377/\nUNIQUE constraint failed: documents_customfieldinstance.document_id, documents_customfieldinstance.field_id\n\nRequest Method: PUT\nRequest URL: http://localhost:8000/api/documents/377/\nDjango Version: 4.2.8\nPython Executable: /Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/bin/python\nPython Version: 3.10.9\nPython Path: ['/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/src', '/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/src', '/Users/mooner/.vscode/extensions/ms-python.python-2023.22.1/pythonFiles/lib/python/debugpy/_vendored/pydevd', '/Users/mooner/.pyenv/versions/3.10.9/lib/python310.zip', '/Users/mooner/.pyenv/versions/3.10.9/lib/python3.10', '/Users/mooner/.pyenv/versions/3.10.9/lib/python3.10/lib-dynload', '/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages']\nServer time: Sun, 07 Jan 2024 10:27:32 -0800\nInstalled Applications:\n['whitenoise.runserver_nostatic',\n 'django.contrib.auth',\n 'django.contrib.contenttypes',\n 'django.contrib.sessions',\n 'django.contrib.messages',\n 'django.contrib.staticfiles',\n 'corsheaders',\n 'django_extensions',\n 'paperless',\n 'documents.apps.DocumentsConfig',\n 'paperless_tesseract.apps.PaperlessTesseractConfig',\n 'paperless_text.apps.PaperlessTextConfig',\n 'paperless_mail.apps.PaperlessMailConfig',\n 'django.contrib.admin',\n 'rest_framework',\n 'rest_framework.authtoken',\n 'django_filters',\n 'django_celery_results',\n 'guardian',\n 'allauth.socialaccount.providers.github',\n 'channels']\nInstalled Middleware:\n['compression_middleware.middleware.CompressionMiddleware',\n 'django.middleware.security.SecurityMiddleware',\n 'whitenoise.middleware.WhiteNoiseMiddleware',\n 'django.contrib.sessions.middleware.SessionMiddleware',\n 'corsheaders.middleware.CorsMiddleware',\n 'django.middleware.locale.LocaleMiddleware',\n 'django.middleware.common.CommonMiddleware',\n 'django.middleware.csrf.CsrfViewMiddleware',\n 'paperless.middleware.ApiVersionMiddleware',\n 'django.contrib.auth.middleware.AuthenticationMiddleware',\n 'django.contrib.messages.middleware.MessageMiddleware',\n 'django.middleware.clickjacking.XFrameOptionsMiddleware']\n\n\nTraceback (most recent call last):\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/django/db/backends/utils.py\", line 89, in _execute\n    return self.cursor.execute(sql, params)\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/django/db/backends/sqlite3/base.py\", line 328, in execute\n    return super().execute(query, params)\n\nThe above exception (UNIQUE constraint failed: documents_customfieldinstance.document_id, documents_customfieldinstance.field_id) was the direct cause of the following exception:\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/django/core/handlers/exception.py\", line 55, in inner\n    response = get_response(request)\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/django/core/handlers/base.py\", line 197, in _get_response\n    response = wrapped_callback(request, *callback_args, **callback_kwargs)\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/django/views/decorators/csrf.py\", line 56, in wrapper_view\n    return view_func(*args, **kwargs)\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/rest_framework/viewsets.py\", line 125, in view\n    return self.dispatch(request, *args, **kwargs)\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/rest_framework/views.py\", line 509, in dispatch\n    response = self.handle_exception(exc)\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/rest_framework/views.py\", line 469, in handle_exception\n    self.raise_uncaught_exception(exc)\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/rest_framework/views.py\", line 480, in raise_uncaught_exception\n    raise exc\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/rest_framework/views.py\", line 506, in dispatch\n    response = handler(request, *args, **kwargs)\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/src/documents/views.py\", line 330, in update\n    document_updated.send(\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/django/dispatch/dispatcher.py\", line 176, in send\n    return [\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/django/dispatch/dispatcher.py\", line 177, in <listcomp>\n    (receiver, receiver(signal=self, sender=sender, **named))\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/src/documents/signals/handlers.py\", line 531, in run_workflow_updated\n    run_workflow(\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/src/documents/signals/handlers.py\", line 613, in run_workflow\n    CustomFieldInstance.objects.create(\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/django/db/models/manager.py\", line 87, in manager_method\n    return getattr(self.get_queryset(), name)(*args, **kwargs)\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/django/db/models/query.py\", line 658, in create\n    obj.save(force_insert=True, using=self.db)\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/django/db/models/base.py\", line 814, in save\n    self.save_base(\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/django/db/models/base.py\", line 877, in save_base\n    updated = self._save_table(\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/django/db/models/base.py\", line 1020, in _save_table\n    results = self._do_insert(\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/django/db/models/base.py\", line 1061, in _do_insert\n    return manager._insert(\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/django/db/models/manager.py\", line 87, in manager_method\n    return getattr(self.get_queryset(), name)(*args, **kwargs)\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/django/db/models/query.py\", line 1805, in _insert\n    return query.get_compiler(using=using).execute_sql(returning_fields)\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/django/db/models/sql/compiler.py\", line 1822, in execute_sql\n    cursor.execute(sql, params)\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/django/db/backends/utils.py\", line 102, in execute\n    return super().execute(sql, params)\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/django/db/backends/utils.py\", line 67, in execute\n    return self._execute_with_wrappers(\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/django/db/backends/utils.py\", line 80, in _execute_with_wrappers\n    return executor(sql, params, many, context)\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/django/db/backends/utils.py\", line 84, in _execute\n    with self.db.wrap_database_errors:\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/django/db/utils.py\", line 91, in __exit__\n    raise dj_exc_value.with_traceback(traceback) from exc_value\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/django/db/backends/utils.py\", line 89, in _execute\n    return self.cursor.execute(sql, params)\n  File \"/Users/mooner/Documents/Work/Dev.nosync/OSS/paperless-ngx/.venv/lib/python3.10/site-packages/django/db/backends/sqlite3/base.py\", line 328, in execute\n    return super().execute(query, params)\n\nException Type: IntegrityError at /api/documents/377/\nException Value: UNIQUE constraint failed: documents_customfieldinstance.document_id, documents_customfieldinstance.field_id\nRaised during: documents.views.UnifiedSearchViewSet\nRequest information:\nUSER: shamoon\n}
```

Resolving patch:
diff --git a/src/documents/signals/handlers.py b/src/documents/signals/handlers.py
--- a/src/documents/signals/handlers.py
+++ b/src/documents/signals/handlers.py
@@ -610,10 +610,18 @@ def run_workflow(
 
                 if action.assign_custom_fields is not None:
                     for field in action.assign_custom_fields.all():
-                        CustomFieldInstance.objects.create(
-                            field=field,
-                            document=document,
-                        )  # adds to document
+                        if (
+                            CustomFieldInstance.objects.filter(
+                                field=field,
+                                document=document,
+                            ).count()
+                            == 0
+                        ):
+                            # can be triggered on existing docs, so only add the field if it doesnt already exist
+                            CustomFieldInstance.objects.create(
+                                field=field,
+                                document=document,
+                            )
 
             document.save()
 


Test patch:
diff --git a/src/documents/tests/test_workflows.py b/src/documents/tests/test_workflows.py
--- a/src/documents/tests/test_workflows.py
+++ b/src/documents/tests/test_workflows.py
@@ -13,6 +13,7 @@
 from documents.matching import document_matches_workflow
 from documents.models import Correspondent
 from documents.models import CustomField
+from documents.models import CustomFieldInstance
 from documents.models import Document
 from documents.models import DocumentType
 from documents.models import MatchingModel
@@ -997,6 +998,47 @@ def test_document_updated_workflow(self):
 
         self.assertEqual(doc.custom_fields.all().count(), 1)
 
+    def test_document_updated_workflow_existing_custom_field(self):
+        """
+        GIVEN:
+            - Existing workflow with UPDATED trigger and action that adds a custom field
+        WHEN:
+            - Document is updated that already contains the field
+        THEN:
+            - Document update succeeds without trying to re-create the field
+        """
+        trigger = WorkflowTrigger.objects.create(
+            type=WorkflowTrigger.WorkflowTriggerType.DOCUMENT_UPDATED,
+            filter_has_document_type=self.dt,
+        )
+        action = WorkflowAction.objects.create()
+        action.assign_custom_fields.add(self.cf1)
+        w = Workflow.objects.create(
+            name="Workflow 1",
+            order=0,
+        )
+        w.triggers.add(trigger)
+        w.actions.add(action)
+        w.save()
+
+        doc = Document.objects.create(
+            title="sample test",
+            correspondent=self.c,
+            original_filename="sample.pdf",
+        )
+        CustomFieldInstance.objects.create(document=doc, field=self.cf1)
+
+        superuser = User.objects.create_superuser("superuser")
+        self.client.force_authenticate(user=superuser)
+
+        self.client.patch(
+            f"/api/documents/{doc.id}/",
+            {"document_type": self.dt.id},
+            format="json",
+        )
+
+        self.assertEqual(doc.custom_fields.all().count(), 1)
+
     def test_workflow_enabled_disabled(self):
         trigger = WorkflowTrigger.objects.create(
             type=WorkflowTrigger.WorkflowTriggerType.DOCUMENT_ADDED,