Repository: adamchainz/django-mysql

Issue:
Crash on check for ListCharField
Python Version: Python 3.6.5
Django Version: Django 2.0.4
Version: Django-MySQL 2.2.1

I use the `ListCharField`

When I execute `python manage.py runserver`, I encountered the following:

```
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/gerry/anaconda3/envs/im2txt_demo/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/home/gerry/anaconda3/envs/im2txt_demo/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/gerry/anaconda3/envs/im2txt_demo/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/gerry/anaconda3/envs/im2txt_demo/lib/python3.6/site-packages/django/core/management/base.py", line 332, in execute
    self.check()
  File "/home/gerry/anaconda3/envs/im2txt_demo/lib/python3.6/site-packages/django/core/management/base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/gerry/anaconda3/envs/im2txt_demo/lib/python3.6/site-packages/django/core/management/base.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/gerry/anaconda3/envs/im2txt_demo/lib/python3.6/site-packages/django/core/checks/registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/gerry/anaconda3/envs/im2txt_demo/lib/python3.6/site-packages/django/core/checks/model_checks.py", line 27, in check_all_models
    errors.extend(model.check(**kwargs))
  File "/home/gerry/anaconda3/envs/im2txt_demo/lib/python3.6/site-packages/django/db/models/base.py", line 1200, in check
    errors.extend(cls._check_fields(**kwargs))
  File "/home/gerry/anaconda3/envs/im2txt_demo/lib/python3.6/site-packages/django/db/models/base.py", line 1272, in _check_fields
    errors.extend(field.check(**kwargs))
  File "/home/gerry/anaconda3/envs/im2txt_demo/lib/python3.6/site-packages/django_mysql/models/fields/lists.py", line 184, in check
    if max_size > self.max_length:
TypeError: '>' not supported between instances of 'int' and 'NoneType'
```
Please help me, Thanks a lot.


Discussion:
You need to specify `max_length` for the field to work. I'll fix this crash though.
In fact, `max_length` is required by `CharField`, and that check is running, just this crash is preventing you from seeing that warning.

Resolving patch:
diff --git a/django_mysql/models/fields/lists.py b/django_mysql/models/fields/lists.py
--- a/django_mysql/models/fields/lists.py
+++ b/django_mysql/models/fields/lists.py
@@ -172,6 +172,7 @@ def check(self, **kwargs):
         has_base_error = any(e.id == 'django_mysql.E004' for e in errors)
         if (
             not has_base_error and
+            self.max_length is not None and
             isinstance(self.base_field, CharField) and
             self.size
         ):
diff --git a/django_mysql/models/fields/sets.py b/django_mysql/models/fields/sets.py
--- a/django_mysql/models/fields/sets.py
+++ b/django_mysql/models/fields/sets.py
@@ -156,6 +156,7 @@ def check(self, **kwargs):
         has_base_error = any(e.id == 'django_mysql.E001' for e in errors)
         if (
             not has_base_error and
+            self.max_length is not None and
             isinstance(self.base_field, CharField) and
             self.size
         ):


Test patch:
diff --git a/tests/testapp/test_listcharfield.py b/tests/testapp/test_listcharfield.py
--- a/tests/testapp/test_listcharfield.py
+++ b/tests/testapp/test_listcharfield.py
@@ -451,6 +451,21 @@ class InvalidListCharModel3(TemporaryModel):
         assert errors[0].id == 'django_mysql.E006'
         assert 'Field can overrun' in errors[0].msg
 
+    def test_max_length_missing_doesnt_crash(self):
+        class InvalidListCharModel4(TemporaryModel):
+            field = ListCharField(
+                models.CharField(max_length=2),
+                size=2,
+            )
+
+        errors = InvalidListCharModel4.check(actually_check=True)
+        assert len(errors) == 1
+        assert errors[0].id == 'fields.E120'
+        assert (
+            errors[0].msg ==
+            "CharFields must define a 'max_length' attribute."
+        )
+
 
 class TestDeconstruct(TestCase):
 
diff --git a/tests/testapp/test_setcharfield.py b/tests/testapp/test_setcharfield.py
--- a/tests/testapp/test_setcharfield.py
+++ b/tests/testapp/test_setcharfield.py
@@ -408,6 +408,21 @@ class InvalidSetCharFieldModel3(TemporaryModel):
         assert errors[0].id == 'django_mysql.E003'
         assert 'Field can overrun' in errors[0].msg
 
+    def test_max_length_missing_doesnt_crash(self):
+        class InvalidSetCharFieldModel4(TemporaryModel):
+            field = SetCharField(
+                models.CharField(max_length=2),
+                size=2,
+            )
+
+        errors = InvalidSetCharFieldModel4.check(actually_check=True)
+        assert len(errors) == 1
+        assert errors[0].id == 'fields.E120'
+        assert (
+            errors[0].msg ==
+            "CharFields must define a 'max_length' attribute."
+        )
+
 
 class TestDeconstruct(TestCase):