Ticket #5725: mysql_varchar_length.diff

File mysql_varchar_length.diff, 2.6 KB (added by ferdonline, 12 years ago)

Patch fixing the incorrectly detected varchar field length

  • django/db/backends/mysql/introspection.py

    diff --git a/django/db/backends/mysql/introspection.py b/django/db/backends/mysql/introspection.py
    index e6f18b8..eec69e4 100644
    a b class DatabaseIntrospection(BaseDatabaseIntrospection):  
    3636    def get_table_description(self, cursor, table_name):
    3737        "Returns a description of the table, with the DB-API cursor.description interface."
    3838        cursor.execute("SELECT * FROM %s LIMIT 1" % self.connection.ops.quote_name(table_name))
    39         return cursor.description
     39        description = list(cursor.description)
     40       
     41        #Fix UTF8 multi-byte charset problem (Ticket #5725)
     42        cursor.execute("SELECT * FROM INFORMATION_SCHEMA.COLUMNS \
     43                       WHERE table_name = '%s' AND table_schema = DATABASE()" % (table_name, ))
     44       
     45        new_info = cursor.fetchall()
     46       
     47        for i, field in enumerate(description):
     48            if field[1] == 253 and new_info[i][8] is not None: #character varying
     49                field = list(field) #Turn mutable
     50                field[3] = int(new_info[i][8])
     51                description[i] = field
     52           
     53        return description
    4054
    4155    def _name_to_index(self, cursor, table_name):
    4256        """
  • tests/regressiontests/introspection/tests.py

    diff --git a/tests/regressiontests/introspection/tests.py b/tests/regressiontests/introspection/tests.py
    index ca58f04..d546bcf 100644
    a b class IntrospectionTests(TestCase):  
    8989            [datatype(r[1], r) for r in desc],
    9090            ['IntegerField', 'CharField', 'CharField', 'CharField', 'BigIntegerField']
    9191        )
     92       
     93       
     94    def test_get_table_description_types_varchar_length(self):
     95       
     96        cursor = connection.cursor()
     97        cursor.execute("""CREATE  TABLE django_ixn_test_table_varchars (
     98                              id INT NOT NULL ,
     99                              name VARCHAR(45) NULL ,
     100                              other_charfield VARCHAR(128) NULL );""")
     101       
     102        desc = connection.introspection.get_table_description( cursor, "django_ixn_test_table_varchars" )
     103        cursor.execute("DROP TABLE django_ixn_test_table_varchars;")
     104       
     105        self.assertEqual(
     106            [ field_desc[3] if field_desc[1]==253 else None for field_desc in desc],
     107            [ None, 45, 128 ]
     108        )
    92109
    93110    # Oracle forces null=True under the hood in some cases (see
    94111    # https://docs.djangoproject.com/en/dev/ref/databases/#null-and-empty-strings)
Back to Top