Skip to content

Commit 9ede371

Browse files
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
1 parent 38b8d18 commit 9ede371

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

django/core/db/backends/sqlite3.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,36 @@ def get_relations(cursor, table_name):
185185
'USStateField': 'varchar(2)',
186186
}
187187

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()

django/core/management.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,9 +591,19 @@ def table2model(table_name):
591591
field_type_was_guessed = True
592592
else:
593593
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+
594605
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()])
597607
field_desc += ')'
598608
if field_type_was_guessed:
599609
field_desc += ' # This is a guess!'

docs/django-admin.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ customizations. In particular, you'll need to do this:
8787
doesn't yet introspect primary keys.
8888

8989
``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.
9291

9392
install [modelmodule modelmodule ...]
9493
-------------------------------------

0 commit comments

Comments
 (0)