From de7a336486f83dd9c01b8c2fd3feb014da7f5a49 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Tue, 29 Nov 2005 02:05:32 +0000 Subject: [PATCH] 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 --- AUTHORS | 1 + django/core/db/__init__.py | 1 + django/core/db/backends/ado_mssql.py | 3 +++ django/core/db/backends/mysql.py | 5 +++++ django/core/db/backends/postgresql.py | 5 +++++ django/core/db/backends/sqlite3.py | 7 ++++++- django/core/management.py | 13 ++++++++----- docs/django-admin.txt | 5 +++-- 8 files changed, 32 insertions(+), 8 deletions(-) diff --git a/AUTHORS b/AUTHORS index 77ed89d7554..a59af03daa1 100644 --- a/AUTHORS +++ b/AUTHORS @@ -74,6 +74,7 @@ answer newbie questions, and generally made Django that much better: David Schein sopel Radek Švarz + Swaroop C H Aaron Swartz Tom Tobin Joe Topjian diff --git a/django/core/db/__init__.py b/django/core/db/__init__.py index f0ffeebc2e7..5883acefddb 100644 --- a/django/core/db/__init__.py +++ b/django/core/db/__init__.py @@ -35,6 +35,7 @@ get_date_trunc_sql = dbmod.get_date_trunc_sql 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 diff --git a/django/core/db/backends/ado_mssql.py b/django/core/db/backends/ado_mssql.py index be215ed7e91..bb5b628acab 100644 --- a/django/core/db/backends/ado_mssql.py +++ b/django/core/db/backends/ado_mssql.py @@ -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 diff --git a/django/core/db/backends/mysql.py b/django/core/db/backends/mysql.py index 10379f95385..0e31af13fc1 100644 --- a/django/core/db/backends/mysql.py +++ b/django/core/db/backends/mysql.py @@ -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 diff --git a/django/core/db/backends/postgresql.py b/django/core/db/backends/postgresql.py index 487dd0da68b..c650660ad8f 100644 --- a/django/core/db/backends/postgresql.py +++ b/django/core/db/backends/postgresql.py @@ -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)} diff --git a/django/core/db/backends/sqlite3.py b/django/core/db/backends/sqlite3.py index 930bb7c2909..9133d08bba8 100644 --- a/django/core/db/backends/sqlite3.py +++ b/django/core/db/backends/sqlite3.py @@ -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 diff --git a/django/core/management.py b/django/core/management.py index 4e6d0b40513..06c31abe13c 100644 --- a/django/core/management.py +++ b/django/core/management.py @@ -571,8 +571,7 @@ def inspectdb(db_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 inspectdb(db_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 '' diff --git a/docs/django-admin.txt b/docs/django-admin.txt index a8dff6defac..79cf4521ca9 100644 --- a/docs/django-admin.txt +++ b/docs/django-admin.txt @@ -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 ...] -------------------------------------