Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #971 -- inspectdb for SQLite now introspects field types.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1518 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 9ede371c85a97406eecb19974ab01163839636a0 1 parent 38b8d18
Adrian Holovaty authored
34  django/core/db/backends/sqlite3.py
@@ -185,4 +185,36 @@ def get_relations(cursor, table_name):
185 185
     'USStateField':                 'varchar(2)',
186 186
 }
187 187
 
188  
-DATA_TYPES_REVERSE = {}
  188
+# Maps SQL types to Django Field types. Some of the SQL types have multiple
  189
+# entries here because SQLite allows for anything and doesn't normalize the
  190
+# field type; it uses whatever was given.
  191
+BASE_DATA_TYPES_REVERSE = {
  192
+    'bool': 'BooleanField',
  193
+    'boolean': 'BooleanField',
  194
+    'smallint': 'SmallIntegerField',
  195
+    'smallinteger': 'SmallIntegerField',
  196
+    'int': 'IntegerField',
  197
+    'integer': 'IntegerField',
  198
+    'text': 'TextField',
  199
+    'char': 'CharField',
  200
+    'date': 'DateField',
  201
+    'datetime': 'DateTimeField',
  202
+    'time': 'TimeField',
  203
+}
  204
+
  205
+# This light wrapper "fakes" a dictionary interface, because some SQLite data
  206
+# types include variables in them -- e.g. "varchar(30)" -- and can't be matched
  207
+# as a simple dictionary lookup.
  208
+class FlexibleFieldLookupDict:
  209
+    def __getitem__(self, key):
  210
+        key = key.lower()
  211
+        try:
  212
+            return BASE_DATA_TYPES_REVERSE[key]
  213
+        except KeyError:
  214
+            import re
  215
+            m = re.search(r'^\s*(?:var)?char\s*\(\s*(\d+)\s*\)\s*$', key)
  216
+            if m:
  217
+                return ('CharField', {'maxlength': m.group(1)})
  218
+            raise KeyError
  219
+
  220
+DATA_TYPES_REVERSE = FlexibleFieldLookupDict()
14  django/core/management.py
@@ -591,9 +591,19 @@ def table2model(table_name):
591 591
                     field_type_was_guessed = True
592 592
                 else:
593 593
                     field_type_was_guessed = False
  594
+
  595
+                # This is a hook for DATA_TYPES_REVERSE to return a tuple of
  596
+                # (field_type, extra_params_dict).
  597
+                if type(field_type) is tuple:
  598
+                    field_type, extra_params = field_type
  599
+                else:
  600
+                    extra_params = {}
  601
+
  602
+                if field_type == 'CharField' and row[3]:
  603
+                    extra_params['maxlength'] = row[3]
  604
+
594 605
                 field_desc = '%s = meta.%s(' % (column_name, field_type)
595  
-                if field_type == 'CharField':
596  
-                    field_desc += 'maxlength=%s' % (row[3])
  606
+                field_desc += ', '.join(['%s=%s' % (k, v) for k, v in extra_params.items()])
597 607
                 field_desc += ')'
598 608
                 if field_type_was_guessed:
599 609
                     field_desc += ' # This is a guess!'
3  docs/django-admin.txt
@@ -87,8 +87,7 @@ customizations. In particular, you'll need to do this:
87 87
       doesn't yet introspect primary keys.
88 88
 
89 89
 ``inspectdb`` works with PostgreSQL, MySQL and SQLite. Foreign-key detection
90  
-only works in PostgreSQL. In SQLite, it cannot detect column types; it'll
91  
-use ``TextField`` for each column.
  90
+only works in PostgreSQL.
92 91
 
93 92
 install [modelmodule modelmodule ...]
94 93
 -------------------------------------

0 notes on commit 9ede371

Please sign in to comment.
Something went wrong with that request. Please try again.