Repository: paperless-ngx/paperless-ngx

Issue:
[BUG] Error ‘value too long for type character varying(3)‘ when saving Consumption templates 
### Description

I have the following Consumption Template:
![image](https://github.com/paperless-ngx/paperless-ngx/assets/54413820/b729a4eb-cdf2-41e8-b344-87b50d2cafc6)

If i save this i got an "Server Error (500)". 

The logs says `psycopg2.errors.StringDataRightTruncation: value too long for type character varying(3)` so i delete 2 of the Source filters (Importordner and E-Mail-Abruf) and i can save the template.

How i can include more (All) sources?

Also, what i need to insert in the "Titel zuweisen" if i want the default behavior for 2.0? Currently i let it empty and the title of new uploaded documents are empty:
![image](https://github.com/paperless-ngx/paperless-ngx/assets/54413820/2aca62f5-487b-40ce-a084-1275d9c96b12)
In the docs i can not find the right placeholder.
[Title Placeholders](https://docs.paperless-ngx.com/usage/#title-placeholders)
Something like {document_name}

Thanks

### Steps to reproduce

Add more than 1 Source

### Webserver logs

```bash
2023/11/30 08:12:50	stderr	
2023/11/30 08:12:50	stderr	django.db.utils.DataError: value too long for type character varying(3)
2023/11/30 08:12:50	stderr	           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023/11/30 08:12:50	stderr	    return self.cursor.execute(sql, params)
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 89, in _execute
2023/11/30 08:12:50	stderr	    raise dj_exc_value.with_traceback(traceback) from exc_value
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/django/db/utils.py", line 91, in __exit__
2023/11/30 08:12:50	stderr	    with self.db.wrap_database_errors:
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 84, in _execute
2023/11/30 08:12:50	stderr	           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023/11/30 08:12:50	stderr	    return executor(sql, params, many, context)
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
2023/11/30 08:12:50	stderr	           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023/11/30 08:12:50	stderr	    return self._execute_with_wrappers(
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 67, in execute
2023/11/30 08:12:50	stderr	    cursor.execute(sql, params)
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1562, in execute_sql
2023/11/30 08:12:50	stderr	             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023/11/30 08:12:50	stderr	    cursor = super().execute_sql(result_type)
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1984, in execute_sql
2023/11/30 08:12:50	stderr	           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023/11/30 08:12:50	stderr	    return query.get_compiler(self.db).execute_sql(CURSOR)
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/django/db/models/query.py", line 1231, in _update
2023/11/30 08:12:50	stderr	           ^^^^^^^^^^^^^^^^^^^^^^^^
2023/11/30 08:12:50	stderr	    return filtered._update(values) > 0
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/django/db/models/base.py", line 1054, in _do_update
2023/11/30 08:12:50	stderr	              ^^^^^^^^^^^^^^^^
2023/11/30 08:12:50	stderr	    updated = self._do_update(
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/django/db/models/base.py", line 990, in _save_table
2023/11/30 08:12:50	stderr	              ^^^^^^^^^^^^^^^^^
2023/11/30 08:12:50	stderr	    updated = self._save_table(
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/django/db/models/base.py", line 877, in save_base
2023/11/30 08:12:50	stderr	    self.save_base(
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/django/db/models/base.py", line 814, in save
2023/11/30 08:12:50	stderr	    instance.save()
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/rest_framework/serializers.py", line 1006, in update
2023/11/30 08:12:50	stderr	                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023/11/30 08:12:50	stderr	    self.instance = self.update(self.instance, validated_data)
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/rest_framework/serializers.py", line 207, in save
2023/11/30 08:12:50	stderr	    serializer.save()
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/rest_framework/mixins.py", line 78, in perform_update
2023/11/30 08:12:50	stderr	    self.perform_update(serializer)
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/rest_framework/mixins.py", line 68, in update
2023/11/30 08:12:50	stderr	               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023/11/30 08:12:50	stderr	    response = handler(request, *args, **kwargs)
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/rest_framework/views.py", line 506, in dispatch
2023/11/30 08:12:50	stderr	    raise exc
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
2023/11/30 08:12:50	stderr	    self.raise_uncaught_exception(exc)
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/rest_framework/views.py", line 469, in handle_exception
2023/11/30 08:12:50	stderr	               ^^^^^^^^^^^^^^^^^^^^^^^^^^
2023/11/30 08:12:50	stderr	    response = self.handle_exception(exc)
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/rest_framework/views.py", line 509, in dispatch
2023/11/30 08:12:50	stderr	           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023/11/30 08:12:50	stderr	    return self.dispatch(request, *args, **kwargs)
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/rest_framework/viewsets.py", line 125, in view
2023/11/30 08:12:50	stderr	           ^^^^^^^^^^^^^^^^^^^^^^^^^^
2023/11/30 08:12:50	stderr	    return view_func(*args, **kwargs)
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/django/views/decorators/csrf.py", line 56, in wrapper_view
2023/11/30 08:12:50	stderr	           ^^^^^^^^^^^^^^^^^^^^^
2023/11/30 08:12:50	stderr	    return func(*args, **kwargs)
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/asgiref/sync.py", line 538, in thread_handler
2023/11/30 08:12:50	stderr	             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023/11/30 08:12:50	stderr	    result = self.fn(*self.args, **self.kwargs)
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/asgiref/current_thread_executor.py", line 40, in run
2023/11/30 08:12:50	stderr	              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023/11/30 08:12:50	stderr	    ret: _R = await loop.run_in_executor(
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/asgiref/sync.py", line 479, in __call__
2023/11/30 08:12:50	stderr	               ^^^^^^^^^^^^^^^^^^^^^^^
2023/11/30 08:12:50	stderr	    response = await wrapped_callback(
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/django/core/handlers/base.py", line 253, in _get_response_async
2023/11/30 08:12:50	stderr	    raise exc_info[1]
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/asgiref/sync.py", line 534, in thread_handler
2023/11/30 08:12:50	stderr	               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023/11/30 08:12:50	stderr	    response = await get_response(request)
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/django/core/handlers/exception.py", line 42, in inner
2023/11/30 08:12:50	stderr	    raise exc_info[1]
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/asgiref/sync.py", line 534, in thread_handler
2023/11/30 08:12:50	stderr	Traceback (most recent call last):
2023/11/30 08:12:50	stderr	
2023/11/30 08:12:50	stderr	The above exception was the direct cause of the following exception:
2023/11/30 08:12:50	stderr	
2023/11/30 08:12:50	stderr	
2023/11/30 08:12:50	stderr	psycopg2.errors.StringDataRightTruncation: value too long for type character varying(3)
2023/11/30 08:12:50	stderr	           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023/11/30 08:12:50	stderr	    return self.cursor.execute(sql, params)
2023/11/30 08:12:50	stderr	  File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 89, in _execute
2023/11/30 08:12:50	stderr	Traceback (most recent call last):
2023/11/30 08:12:50	stderr	[2023-11-30 08:12:50,396] [ERROR] [django.request] Internal Server Error: /api/consumption_templates/1/
```


### Browser logs

```bash
{"headers":{"normalizedNames":{},"lazyUpdate":null},"status":500,"statusText":"OK","url":"https://SERVER/api/consumption_templates/","ok":false,"name":"HttpErrorResponse","message":"Http failure response for https://SERVER/api/consumption_templates/: 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.0

### Host OS

Synology

### Installation method

Docker - official image

### Browser

_No response_

### Configuration changes

_No response_

### Other

_No response_


Discussion:
If you look in the browser network tab when saving, what is the data thats being sent (e.g. the 'request'), something like
```json
{"id":1,"name":"Test","order":1,"sources":[1,2,3],"filter_path":null,"filter_filename":null,"filter_mailrule":null,"assign_title":null,"assign_tags":[],"assign_correspondent":null,"assign_document_type":null,"assign_storage_path":null,"assign_owner":2,"assign_view_users":[],"assign_view_groups":[],"assign_change_users":[],"assign_change_groups":[],"permissions_form":{},"account":null}
```
(because I cant reproduce this)
![4715](https://github.com/paperless-ngx/paperless-ngx/assets/4887959/b993aadd-c872-4a7a-95c1-7db3e122af95)

I guess you mean this:
![image](https://github.com/paperless-ngx/paperless-ngx/assets/54413820/1aea9032-c19d-46c9-8dcc-343e84236f9f)
`{
  "id": 1,
  "name": "Standard",
  "order": 1,
  "sources": [
    2,
    1,
    3
  ],
  "filter_path": "*",
  "filter_filename": "*",
  "filter_mailrule": null,
  "assign_title": "",
  "assign_tags": [
    1
  ],
  "assign_correspondent": null,
  "assign_document_type": null,
  "assign_storage_path": 2,
  "assign_owner": null,
  "assign_view_users": [],
  "assign_view_groups": [
    1
  ],
  "assign_change_users": [],
  "assign_change_groups": [
    1
  ],
  "permissions_form": {},
  "account": null
}`
What db type are you using?
Postgresql in Version 16 (docker Tag latest)
![image](https://github.com/paperless-ngx/paperless-ngx/assets/54413820/1af1a9b9-6d2e-4354-8af5-8ade1c8261a8)

Perhaps we need to increase the field length to 5 because `1,2,3` is 5 characters, ya know, commas. My guess is Postgres cares about this more than other db types
i changed the defintion with `ALTER TABLE documents_consumptiontemplate ALTER COLUMN sources TYPE varchar(5);`

and yeah.. now it works:
![image](https://github.com/paperless-ngx/paperless-ngx/assets/54413820/3072dfec-5ca7-4234-8f7c-f12ddaeec64f)


Resolving patch:
diff --git a/src/documents/migrations/1041_alter_consumptiontemplate_sources.py b/src/documents/migrations/1041_alter_consumptiontemplate_sources.py
new file mode 100644
--- /dev/null
+++ b/src/documents/migrations/1041_alter_consumptiontemplate_sources.py
@@ -0,0 +1,22 @@
+# Generated by Django 4.2.7 on 2023-11-30 14:29
+
+import multiselectfield.db.fields
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+    dependencies = [
+        ("documents", "1040_customfield_customfieldinstance_and_more"),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name="consumptiontemplate",
+            name="sources",
+            field=multiselectfield.db.fields.MultiSelectField(
+                choices=[(1, "Consume Folder"), (2, "Api Upload"), (3, "Mail Fetch")],
+                default="1,2,3",
+                max_length=5,
+            ),
+        ),
+    ]
diff --git a/src/documents/models.py b/src/documents/models.py
--- a/src/documents/models.py
+++ b/src/documents/models.py
@@ -754,7 +754,7 @@ class DocumentSourceChoices(models.IntegerChoices):
     order = models.IntegerField(_("order"), default=0)
 
     sources = MultiSelectField(
-        max_length=3,
+        max_length=5,
         choices=DocumentSourceChoices.choices,
         default=f"{DocumentSource.ConsumeFolder},{DocumentSource.ApiUpload},{DocumentSource.MailFetch}",
     )


Test patch: