Repository: django-cms/django-filer

Issue:
Applying filer.0012_file_mime_type: psycopg2.errors.NotNullViolation: null value in column "mime_type" violates not-null constraint
It seems the new mime_type field does not allow NULL values, but all past values will be NULL on first migration. I think this needs a data migration to set all mime_type values for all fields, or to allow NULL values in the field.

Here is a traceback:

```python
Applying filer.0012_file_mime_type...Traceback (most recent call last):
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.NotNullViolation: null value in column "mime_type" violates not-null constraint
DETAIL:  Failing row contains (3179, 35, filer_public/2014/06/20/gpa3.des, 1411, f, GPA3.DES, , null, 2014-06-20 17:33:17.782107+00, 2014-06-20 17:33:54.696439+00, null, t, 871ac8b74cbc7a8f53c3d9ebbc02fe318af88fcd, 25, null).


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 12, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 234, in handle
    fake_initial=fake_initial,
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/django/db/migrations/operations/special.py", line 190, in database_forwards
    self.code(from_state.apps, schema_editor)
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/filer/migrations/0012_file_mime_type.py", line 14, in guess_mimetypes
    file_obj.save(update_fields=['mime_type'])
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/django/db/models/base.py", line 744, in save
    force_update=force_update, update_fields=update_fields)
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/django/db/models/base.py", line 782, in save_base
    force_update, using, update_fields,
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/django/db/models/base.py", line 854, in _save_table
    forced_update)
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/django/db/models/base.py", line 903, in _do_update
    return filtered._update(values) > 0
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/django/db/models/query.py", line 760, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1471, in execute_sql
    cursor = super().execute_sql(result_type)
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1142, in execute_sql
    cursor.execute(sql, params)
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
    return super().execute(sql, params)
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/Users/goettsch/.virtualenvs/are.ucdavis.edu/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: null value in column "mime_type" violates not-null constraint
DETAIL:  Failing row contains (3179, 35, filer_public/2014/06/20/gpa3.des, 1411, f, GPA3.DES, , null, 2014-06-20 17:33:17.782107+00, 2014-06-20 17:33:54.696439+00, null, t, 871ac8b74cbc7a8f53c3d9ebbc02fe318af88fcd, 25, null).
```


Discussion:
I have this problem too

Resolving patch:
diff --git a/filer/__init__.py b/filer/__init__.py
--- a/filer/__init__.py
+++ b/filer/__init__.py
@@ -14,6 +14,6 @@
 10. twine upload dist/django-filer-{new version}.tar.gz
 """
 
-__version__ = '2.0.0'
+__version__ = '2.0.1'
 
 default_app_config = 'filer.apps.FilerConfig'
diff --git a/filer/migrations/0012_file_mime_type.py b/filer/migrations/0012_file_mime_type.py
--- a/filer/migrations/0012_file_mime_type.py
+++ b/filer/migrations/0012_file_mime_type.py
@@ -1,4 +1,4 @@
-# Generated by Django 2.1.9 on 2019-06-25 19:12
+# Generated by Django 3.0.8 on 2020-09-04 10:13
 
 import mimetypes
 
@@ -10,8 +10,10 @@
 def guess_mimetypes(apps, schema_editor):
     FileModel = apps.get_model('filer', 'File')
     for file_obj in FileModel.objects.all():
-        file_obj.mime_type, _ = mimetypes.guess_type(file_obj.file.url)
-        file_obj.save(update_fields=['mime_type'])
+        mime_type, _ = mimetypes.guess_type(file_obj.file.url)
+        if mime_type:
+            file_obj.mime_type = mime_type
+            file_obj.save(update_fields=['mime_type'])
 
 
 class Migration(migrations.Migration):


Test patch: