Repository: django-extensions/django-extensions

Issue:
sqldiff Getting wrong varchar length on MySQL
From the code, I can see the cause: 

```
 def get_field_db_type(self, description, field=None, table_name=None):
    from MySQLdb.constants import FIELD_TYPE
    # weird bug? in mysql db-api where it returns three times the correct value for field length
    # if i remember correctly it had something todo with unicode strings
    # TODO: Fix this is a more meaningful and better understood manner
    description = list(description)
    if description[1] not in [FIELD_TYPE.TINY, FIELD_TYPE.SHORT]:  # exclude tinyints from conversion.
        description[3] = description[3] / 3
        description[4] = description[4] / 3
```

Which generates: |--+ field 'name' parameters differ: db='varchar(85)', model='varchar(256)'

I am using mysql but seems the weird bug not showing up. Anyone help?



Discussion:
I am afraid we need somebody here with a good understanding of MySQL and Python and some time to figure this out. For me I'm mostly lacking in the later.

Would it be an idea to try and raise some money for these types of issues via something like bountysource.com ?

I too am running into this issue.

>    # weird bug? in mysql db-api where it returns three times the correct value for field length
>    # if i remember correctly it had something todo with unicode strings

Is there a URL that describes this bug in detail? I would like to get a better understanding of the issue.

@jdufresne sorry I am not able to find more information about this.

As stated in the header of sqldiff.py MySQL drivers had a lot of problems with introspecting the database when sqldiff was first written. But this was quite a lot time ago (2008), maybe the current state of MySQL drivers for Python have improved things ?

I really like the concept of this tool and want to help make it more useful for MySQL. I think it could be very useful for me as I'm integrating Django with an existing application. I've been experimenting with the code and have been noticing what I could possibly improve.

I've tried commenting out this particular block of code. When commented `sqldiff` produces better, more accurate results for me using MySQL. Unless there is a known case where this is required to produce correct results (that I can then test against) I suggest it be removed. Once removed, `varchar` lengths are accurately detected for me.

I'm testing with Django 1.6, latest django-extensions, and MySQL 5.5.38.


Resolving patch:
diff --git a/django_extensions/management/commands/sqldiff.py b/django_extensions/management/commands/sqldiff.py
--- a/django_extensions/management/commands/sqldiff.py
+++ b/django_extensions/management/commands/sqldiff.py
@@ -537,13 +537,6 @@ class MySQLDiff(SQLDiff):
     # that this is way unreliable for MySQL atm.
     def get_field_db_type(self, description, field=None, table_name=None):
         from MySQLdb.constants import FIELD_TYPE
-        # weird bug? in mysql db-api where it returns three times the correct value for field length
-        # if i remember correctly it had something todo with unicode strings
-        # TODO: Fix this is a more meaningful and better understood manner
-        description = list(description)
-        if description[1] not in [FIELD_TYPE.TINY, FIELD_TYPE.SHORT]:  # exclude tinyints from conversion.
-            description[3] = description[3] / 3
-            description[4] = description[4] / 3
         db_type = super(MySQLDiff, self).get_field_db_type(description)
         if not db_type:
             return


Test patch: