Fixed #460 -- Added 'django-admin.py inspectdb' support for SQLite. Thanks, Swaroop

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1484 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2005-11-29 02:05:32 +00:00
parent 539e5af17c
commit de7a336486
8 changed files with 32 additions and 8 deletions

View File

@ -74,6 +74,7 @@ answer newbie questions, and generally made Django that much better:
David Schein David Schein
sopel sopel
Radek Švarz <http://www.svarz.cz/translate/> Radek Švarz <http://www.svarz.cz/translate/>
Swaroop C H <http://www.swaroopch.info>
Aaron Swartz <http://www.aaronsw.com/> Aaron Swartz <http://www.aaronsw.com/>
Tom Tobin Tom Tobin
Joe Topjian <http://joe.terrarum.net/geek/code/python/django/> Joe Topjian <http://joe.terrarum.net/geek/code/python/django/>

View File

@ -35,6 +35,7 @@ get_date_trunc_sql = dbmod.get_date_trunc_sql
get_limit_offset_sql = dbmod.get_limit_offset_sql get_limit_offset_sql = dbmod.get_limit_offset_sql
get_random_function_sql = dbmod.get_random_function_sql get_random_function_sql = dbmod.get_random_function_sql
get_table_list = dbmod.get_table_list get_table_list = dbmod.get_table_list
get_table_description = dbmod.get_table_description
get_relations = dbmod.get_relations get_relations = dbmod.get_relations
OPERATOR_MAPPING = dbmod.OPERATOR_MAPPING OPERATOR_MAPPING = dbmod.OPERATOR_MAPPING
DATA_TYPES = dbmod.DATA_TYPES DATA_TYPES = dbmod.DATA_TYPES

View File

@ -112,6 +112,9 @@ def get_random_function_sql():
def get_table_list(cursor): def get_table_list(cursor):
raise NotImplementedError raise NotImplementedError
def get_table_description(cursor, table_name):
raise NotImplementedError
def get_relations(cursor, table_name): def get_relations(cursor, table_name):
raise NotImplementedError raise NotImplementedError

View File

@ -124,6 +124,11 @@ def get_table_list(cursor):
cursor.execute("SHOW TABLES") cursor.execute("SHOW TABLES")
return [row[0] for row in cursor.fetchall()] return [row[0] for row in cursor.fetchall()]
def get_table_description(cursor, table_name):
"Returns a description of the table, with the DB-API cursor.description interface."
cursor.execute("SELECT * FROM %s LIMIT 1" % table_name)
return cursor.description
def get_relations(cursor, table_name): def get_relations(cursor, table_name):
raise NotImplementedError raise NotImplementedError

View File

@ -100,6 +100,11 @@ def get_table_list(cursor):
AND pg_catalog.pg_table_is_visible(c.oid)""") AND pg_catalog.pg_table_is_visible(c.oid)""")
return [row[0] for row in cursor.fetchall()] return [row[0] for row in cursor.fetchall()]
def get_table_description(cursor, table_name):
"Returns a description of the table, with the DB-API cursor.description interface."
cursor.execute("SELECT * FROM %s LIMIT 1" % table_name)
return cursor.description
def get_relations(cursor, table_name): def get_relations(cursor, table_name):
""" """
Returns a dictionary of {field_index: (field_index_other_table, other_table)} Returns a dictionary of {field_index: (field_index_other_table, other_table)}

View File

@ -124,7 +124,12 @@ def _sqlite_date_trunc(lookup_type, dt):
return "%i-%02i-%02i 00:00:00" % (dt.year, dt.month, dt.day) return "%i-%02i-%02i 00:00:00" % (dt.year, dt.month, dt.day)
def get_table_list(cursor): def get_table_list(cursor):
raise NotImplementedError cursor.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")
return [row[0] for row in cursor.fetchall()]
def get_table_description(cursor, table_name):
cursor.execute("PRAGMA table_info(%s)" % table_name)
return [(row[1], row[2], None, None) for row in cursor.fetchall()]
def get_relations(cursor, table_name): def get_relations(cursor, table_name):
raise NotImplementedError raise NotImplementedError

View File

@ -571,8 +571,7 @@ def inspectdb(db_name):
relations = db.get_relations(cursor, table_name) relations = db.get_relations(cursor, table_name)
except NotImplementedError: except NotImplementedError:
relations = {} relations = {}
cursor.execute("SELECT * FROM %s LIMIT 1" % table_name) for i, row in enumerate(db.get_table_description(cursor, table_name)):
for i, row in enumerate(cursor.description):
column_name = row[0] column_name = row[0]
if relations.has_key(i): if relations.has_key(i):
rel = relations[i] rel = relations[i]
@ -586,12 +585,16 @@ def inspectdb(db_name):
field_type = db.DATA_TYPES_REVERSE[row[1]] field_type = db.DATA_TYPES_REVERSE[row[1]]
except KeyError: except KeyError:
field_type = 'TextField' field_type = 'TextField'
yield " # The model-creator script used TextField by default, because" field_type_was_guessed = True
yield " # it couldn't recognize your field type." else:
field_type_was_guessed = False
field_desc = '%s = meta.%s(' % (column_name, field_type) field_desc = '%s = meta.%s(' % (column_name, field_type)
if field_type == 'CharField': if field_type == 'CharField':
field_desc += 'maxlength=%s' % (row[3]) field_desc += 'maxlength=%s' % (row[3])
yield ' %s)' % field_desc field_desc += ')'
if field_type_was_guessed:
field_desc += ' # This is a guess!'
yield ' %s' % field_desc
yield ' class META:' yield ' class META:'
yield ' db_table = %r' % table_name yield ' db_table = %r' % table_name
yield '' yield ''

View File

@ -86,8 +86,9 @@ customizations. In particular, you'll need to do this:
* Add ``primary_key=True`` to one field in each model. The ``inspectdb`` * Add ``primary_key=True`` to one field in each model. The ``inspectdb``
doesn't yet introspect primary keys. doesn't yet introspect primary keys.
``inspectdb`` only works with PostgreSQL and MySQL. Foreign-key detection only ``inspectdb`` works with PostgreSQL, MySQL and SQLite. Foreign-key detection
works in PostgreSQL. only works in PostgreSQL. In SQLite, it cannot detect column types; it'll
use ``TextField`` for each column.
install [modelmodule modelmodule ...] install [modelmodule modelmodule ...]
------------------------------------- -------------------------------------