Code

Ticket #5014: 5014-with-tests.diff

File 5014-with-tests.diff, 2.6 KB (added by claudep, 3 years ago)

Updated/completed patch with tests

Line 
1diff --git a/django/core/management/commands/inspectdb.py b/django/core/management/commands/inspectdb.py
2index 5f0e278..e6d142b 100644
3--- a/django/core/management/commands/inspectdb.py
4+++ b/django/core/management/commands/inspectdb.py
5@@ -151,8 +151,13 @@ class Command(NoArgsCommand):
6             field_params['max_length'] = row[3]
7 
8         if field_type == 'DecimalField':
9-            field_params['max_digits'] = row[4]
10-            field_params['decimal_places'] = row[5]
11+            if not row[4] or not row[5]:
12+                field_notes.append('''max_digits and decimal_places have been guessed, as this database handles decimal fields as float''')
13+                max_digits, decimal_places = 10, 5
14+            else:
15+                max_digits, decimal_places = row[4], row[5]
16+            field_params['max_digits'] = max_digits
17+            field_params['decimal_places'] = decimal_places
18 
19         return field_type, field_params, field_notes
20 
21diff --git a/django/db/backends/sqlite3/introspection.py b/django/db/backends/sqlite3/introspection.py
22index 5ee7b64..d34a06b 100644
23--- a/django/db/backends/sqlite3/introspection.py
24+++ b/django/db/backends/sqlite3/introspection.py
25@@ -20,6 +20,7 @@ class FlexibleFieldLookupDict(object):
26         'integer unsigned': 'PositiveIntegerField',
27         'decimal': 'DecimalField',
28         'real': 'FloatField',
29+        'numeric': 'FloatField',
30         'text': 'TextField',
31         'char': 'CharField',
32         'date': 'DateField',
33diff --git a/tests/regressiontests/inspectdb/models.py b/tests/regressiontests/inspectdb/models.py
34index fc2548b..0111cc5 100644
35--- a/tests/regressiontests/inspectdb/models.py
36+++ b/tests/regressiontests/inspectdb/models.py
37@@ -6,3 +6,6 @@ class People(models.Model):
38 
39 class Message(models.Model):
40     from_field = models.ForeignKey(People, db_column='from_id')
41+
42+class DecimalTest(models.Model):
43+    dec_field = models.DecimalField(max_digits=10, decimal_places=5)
44diff --git a/tests/regressiontests/inspectdb/tests.py b/tests/regressiontests/inspectdb/tests.py
45index 683e6e9..5b5cf39 100644
46--- a/tests/regressiontests/inspectdb/tests.py
47+++ b/tests/regressiontests/inspectdb/tests.py
48@@ -14,3 +14,8 @@ class InspectDBTestCase(TestCase):
49         self.assertNotIn("from = models.ForeignKey(InspectdbPeople)", out.getvalue(), msg=error_message)
50         self.assertIn("from_field = models.ForeignKey(InspectdbPeople)", out.getvalue())
51         out.close()
52+
53+    def test_decimal_field(self):
54+        out = StringIO()
55+        call_command('inspectdb', stdout=out)
56+        self.assertIn("dec_field = models.DecimalField(max_digits=10, decimal_places=5)", out.getvalue())