Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 authored
1  AUTHORS
@@ -74,6 +74,7 @@ answer newbie questions, and generally made Django that much better:
74 74
     David Schein
75 75
     sopel
76 76
     Radek Švarz <http://www.svarz.cz/translate/>
  77
+    Swaroop C H <http://www.swaroopch.info>
77 78
     Aaron Swartz <http://www.aaronsw.com/>
78 79
     Tom Tobin
79 80
     Joe Topjian <http://joe.terrarum.net/geek/code/python/django/>
1  django/core/db/__init__.py
@@ -35,6 +35,7 @@
35 35
 get_limit_offset_sql = dbmod.get_limit_offset_sql
36 36
 get_random_function_sql = dbmod.get_random_function_sql
37 37
 get_table_list = dbmod.get_table_list
  38
+get_table_description = dbmod.get_table_description
38 39
 get_relations = dbmod.get_relations
39 40
 OPERATOR_MAPPING = dbmod.OPERATOR_MAPPING
40 41
 DATA_TYPES = dbmod.DATA_TYPES
3  django/core/db/backends/ado_mssql.py
@@ -112,6 +112,9 @@ def get_random_function_sql():
112 112
 def get_table_list(cursor):
113 113
     raise NotImplementedError
114 114
 
  115
+def get_table_description(cursor, table_name):
  116
+    raise NotImplementedError
  117
+
115 118
 def get_relations(cursor, table_name):
116 119
     raise NotImplementedError
117 120
 
5  django/core/db/backends/mysql.py
@@ -124,6 +124,11 @@ def get_table_list(cursor):
124 124
     cursor.execute("SHOW TABLES")
125 125
     return [row[0] for row in cursor.fetchall()]
126 126
 
  127
+def get_table_description(cursor, table_name):
  128
+    "Returns a description of the table, with the DB-API cursor.description interface."
  129
+    cursor.execute("SELECT * FROM %s LIMIT 1" % table_name)
  130
+    return cursor.description
  131
+
127 132
 def get_relations(cursor, table_name):
128 133
     raise NotImplementedError
129 134
 
5  django/core/db/backends/postgresql.py
@@ -100,6 +100,11 @@ def get_table_list(cursor):
100 100
             AND pg_catalog.pg_table_is_visible(c.oid)""")
101 101
     return [row[0] for row in cursor.fetchall()]
102 102
 
  103
+def get_table_description(cursor, table_name):
  104
+    "Returns a description of the table, with the DB-API cursor.description interface."
  105
+    cursor.execute("SELECT * FROM %s LIMIT 1" % table_name)
  106
+    return cursor.description
  107
+
103 108
 def get_relations(cursor, table_name):
104 109
     """
105 110
     Returns a dictionary of {field_index: (field_index_other_table, other_table)}
7  django/core/db/backends/sqlite3.py
@@ -124,7 +124,12 @@ def _sqlite_date_trunc(lookup_type, dt):
124 124
         return "%i-%02i-%02i 00:00:00" % (dt.year, dt.month, dt.day)
125 125
 
126 126
 def get_table_list(cursor):
127  
-    raise NotImplementedError
  127
+    cursor.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")
  128
+    return [row[0] for row in cursor.fetchall()]
  129
+
  130
+def get_table_description(cursor, table_name):
  131
+    cursor.execute("PRAGMA table_info(%s)" % table_name)
  132
+    return [(row[1], row[2], None, None) for row in cursor.fetchall()]
128 133
 
129 134
 def get_relations(cursor, table_name):
130 135
     raise NotImplementedError
13  django/core/management.py
@@ -571,8 +571,7 @@ def table2model(table_name):
571 571
             relations = db.get_relations(cursor, table_name)
572 572
         except NotImplementedError:
573 573
             relations = {}
574  
-        cursor.execute("SELECT * FROM %s LIMIT 1" % table_name)
575  
-        for i, row in enumerate(cursor.description):
  574
+        for i, row in enumerate(db.get_table_description(cursor, table_name)):
576 575
             column_name = row[0]
577 576
             if relations.has_key(i):
578 577
                 rel = relations[i]
@@ -586,12 +585,16 @@ def table2model(table_name):
586 585
                     field_type = db.DATA_TYPES_REVERSE[row[1]]
587 586
                 except KeyError:
588 587
                     field_type = 'TextField'
589  
-                    yield "    # The model-creator script used TextField by default, because"
590  
-                    yield "    # it couldn't recognize your field type."
  588
+                    field_type_was_guessed = True
  589
+                else:
  590
+                    field_type_was_guessed = False
591 591
                 field_desc = '%s = meta.%s(' % (column_name, field_type)
592 592
                 if field_type == 'CharField':
593 593
                     field_desc += 'maxlength=%s' % (row[3])
594  
-            yield '    %s)' % field_desc
  594
+                field_desc += ')'
  595
+                if field_type_was_guessed:
  596
+                    field_desc += ' # This is a guess!'
  597
+            yield '    %s' % field_desc
595 598
         yield '    class META:'
596 599
         yield '        db_table = %r' % table_name
597 600
         yield ''
5  docs/django-admin.txt
@@ -86,8 +86,9 @@ customizations. In particular, you'll need to do this:
86 86
     * Add ``primary_key=True`` to one field in each model. The ``inspectdb``
87 87
       doesn't yet introspect primary keys.
88 88
 
89  
-``inspectdb`` only works with PostgreSQL and MySQL. Foreign-key detection only
90  
-works in PostgreSQL.
  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.
91 92
 
92 93
 install [modelmodule modelmodule ...]
93 94
 -------------------------------------

0 notes on commit de7a336

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