Ticket #5014: 5014-with-tests-2.diff

File 5014-with-tests-2.diff, 3.6 KB (added by Claude Paroz, 14 years ago)

Same patch with mysql introspection fix

  • django/core/management/commands/inspectdb.py

    diff --git a/django/core/management/commands/inspectdb.py b/django/core/management/commands/inspectdb.py
    index 5f0e278..e6d142b 100644
    a b class Command(NoArgsCommand):  
    151151            field_params['max_length'] = row[3]
    152152
    153153        if field_type == 'DecimalField':
    154             field_params['max_digits'] = row[4]
    155             field_params['decimal_places'] = row[5]
     154            if not row[4] or not row[5]:
     155                field_notes.append('''max_digits and decimal_places have been guessed, as this database handles decimal fields as float''')
     156                max_digits, decimal_places = 10, 5
     157            else:
     158                max_digits, decimal_places = row[4], row[5]
     159            field_params['max_digits'] = max_digits
     160            field_params['decimal_places'] = decimal_places
    156161
    157162        return field_type, field_params, field_notes
    158163
  • django/db/backends/mysql/introspection.py

    diff --git a/django/db/backends/mysql/introspection.py b/django/db/backends/mysql/introspection.py
    index 9e1518b..b1893d4 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        # Decimal fields return length + 2, see #5014
     40        fixed_description = [list(col_info) for col_info in cursor.description]
     41        for col_info in fixed_description:
     42            if col_info[1] in (FIELD_TYPE.DECIMAL, FIELD_TYPE.NEWDECIMAL):
     43                col_info[4] -= 2
     44        return fixed_description
    4045
    4146    def _name_to_index(self, cursor, table_name):
    4247        """
  • django/db/backends/sqlite3/introspection.py

    diff --git a/django/db/backends/sqlite3/introspection.py b/django/db/backends/sqlite3/introspection.py
    index 5ee7b64..d34a06b 100644
    a b class FlexibleFieldLookupDict(object):  
    2020        'integer unsigned': 'PositiveIntegerField',
    2121        'decimal': 'DecimalField',
    2222        'real': 'FloatField',
     23        'numeric': 'FloatField',
    2324        'text': 'TextField',
    2425        'char': 'CharField',
    2526        'date': 'DateField',
  • tests/regressiontests/inspectdb/models.py

    diff --git a/tests/regressiontests/inspectdb/models.py b/tests/regressiontests/inspectdb/models.py
    index fc2548b..0111cc5 100644
    a b class People(models.Model):  
    66
    77class Message(models.Model):
    88    from_field = models.ForeignKey(People, db_column='from_id')
     9
     10class DecimalTest(models.Model):
     11    dec_field = models.DecimalField(max_digits=10, decimal_places=5)
  • tests/regressiontests/inspectdb/tests.py

    diff --git a/tests/regressiontests/inspectdb/tests.py b/tests/regressiontests/inspectdb/tests.py
    index 683e6e9..5b5cf39 100644
    a b class InspectDBTestCase(TestCase):  
    1414        self.assertNotIn("from = models.ForeignKey(InspectdbPeople)", out.getvalue(), msg=error_message)
    1515        self.assertIn("from_field = models.ForeignKey(InspectdbPeople)", out.getvalue())
    1616        out.close()
     17
     18    def test_decimal_field(self):
     19        out = StringIO()
     20        call_command('inspectdb', stdout=out)
     21        self.assertIn("dec_field = models.DecimalField(max_digits=10, decimal_places=5)", out.getvalue())
Back to Top