Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #460 -- Added 'django-admin.py inspectdb' support for SQLite. T…

…hanks, Swaroop

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1484 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit de7a336486f83dd9c01b8c2fd3feb014da7f5a49 1 parent 539e5af
Adrian Holovaty adrianholovaty authored
1  AUTHORS
View
@@ -74,6 +74,7 @@ answer newbie questions, and generally made Django that much better:
David Schein
sopel
Radek Švarz <http://www.svarz.cz/translate/>
+ Swaroop C H <http://www.swaroopch.info>
Aaron Swartz <http://www.aaronsw.com/>
Tom Tobin
Joe Topjian <http://joe.terrarum.net/geek/code/python/django/>
1  django/core/db/__init__.py
View
@@ -35,6 +35,7 @@
get_limit_offset_sql = dbmod.get_limit_offset_sql
get_random_function_sql = dbmod.get_random_function_sql
get_table_list = dbmod.get_table_list
+get_table_description = dbmod.get_table_description
get_relations = dbmod.get_relations
OPERATOR_MAPPING = dbmod.OPERATOR_MAPPING
DATA_TYPES = dbmod.DATA_TYPES
3  django/core/db/backends/ado_mssql.py
View
@@ -112,6 +112,9 @@ def get_random_function_sql():
def get_table_list(cursor):
raise NotImplementedError
+def get_table_description(cursor, table_name):
+ raise NotImplementedError
+
def get_relations(cursor, table_name):
raise NotImplementedError
5 django/core/db/backends/mysql.py
View
@@ -124,6 +124,11 @@ def get_table_list(cursor):
cursor.execute("SHOW TABLES")
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):
raise NotImplementedError
5 django/core/db/backends/postgresql.py
View
@@ -100,6 +100,11 @@ def get_table_list(cursor):
AND pg_catalog.pg_table_is_visible(c.oid)""")
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):
"""
Returns a dictionary of {field_index: (field_index_other_table, other_table)}
7 django/core/db/backends/sqlite3.py
View
@@ -124,7 +124,12 @@ def _sqlite_date_trunc(lookup_type, dt):
return "%i-%02i-%02i 00:00:00" % (dt.year, dt.month, dt.day)
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):
raise NotImplementedError
13 django/core/management.py
View
@@ -571,8 +571,7 @@ def table2model(table_name):
relations = db.get_relations(cursor, table_name)
except NotImplementedError:
relations = {}
- cursor.execute("SELECT * FROM %s LIMIT 1" % table_name)
- for i, row in enumerate(cursor.description):
+ for i, row in enumerate(db.get_table_description(cursor, table_name)):
column_name = row[0]
if relations.has_key(i):
rel = relations[i]
@@ -586,12 +585,16 @@ def table2model(table_name):
field_type = db.DATA_TYPES_REVERSE[row[1]]
except KeyError:
field_type = 'TextField'
- yield " # The model-creator script used TextField by default, because"
- yield " # it couldn't recognize your field type."
+ field_type_was_guessed = True
+ else:
+ field_type_was_guessed = False
field_desc = '%s = meta.%s(' % (column_name, field_type)
if field_type == 'CharField':
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 ' db_table = %r' % table_name
yield ''
5 docs/django-admin.txt
View
@@ -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``
doesn't yet introspect primary keys.
-``inspectdb`` only works with PostgreSQL and MySQL. Foreign-key detection only
-works in PostgreSQL.
+``inspectdb`` works with PostgreSQL, MySQL and SQLite. Foreign-key detection
+only works in PostgreSQL. In SQLite, it cannot detect column types; it'll
+use ``TextField`` for each column.
install [modelmodule modelmodule ...]
-------------------------------------
Please sign in to comment.
Something went wrong with that request. Please try again.