Ticket #2272: django-sqlite-introspection.diff

File django-sqlite-introspection.diff, 3.6 KB (added by dne@…, 18 years ago)
  • django/db/backends/sqlite3/introspection.py

     
    22from django.db.backends.sqlite3.base import quote_name
    33
    44def get_table_list(cursor):
    5     cursor.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")
     5    "Returns a list of table names in the current database."
     6    # skip the sqlite_sequence system table used for autoincrement key
     7    # generation
     8    cursor.execute("""
     9        SELECT name FROM sqlite_master
     10        WHERE type='table' AND NOT name='sqlite_sequence' ORDER BY name""")
    611    return [row[0] for row in cursor.fetchall()]
    712
    813def get_table_description(cursor, table_name):
    9     cursor.execute("PRAGMA table_info(%s)" % quote_name(table_name))
    10     return [(row[1], row[2], None, None) for row in cursor.fetchall()]
     14    "Returns a description of the table, with the DB-API cursor.description interface."
     15    return [(info['name'], info['type'], None, None, None, None,
     16             info['null_ok']) for info in _table_info(cursor, table_name)]
    1117
    1218def get_relations(cursor, table_name):
    1319    raise NotImplementedError
    1420
    1521def get_indexes(cursor, table_name):
    16     raise NotImplementedError
     22    """
     23    Returns a dictionary of fieldname -> infodict for the given table,
     24    where each infodict is in the format:
     25        {'primary_key': boolean representing whether it's the primary key,
     26         'unique': boolean representing whether it's a unique index}
     27    """
     28    indexes = {}
     29    for info in _table_info(cursor, table_name):
     30        indexes[info['name']] = {'primary_key': info['pk'] != 0,
     31                                 'unique': False}
     32    cursor.execute('PRAGMA index_list(%s)' % quote_name(table_name))
     33    # seq, name, unique
     34    for index, unique in [(field[1], field[2]) for field in cursor.fetchall()]:
     35        if not unique:
     36            continue
     37        cursor.execute('PRAGMA index_info(%s)' % quote_name(index))
     38        info = cursor.fetchall()
     39        # skip indexes across multiple fields
     40        if len(info) != 1:
     41            continue
     42        name = info[0][2] # seqno, cid, name
     43        indexes[name]['unique'] = True
     44    return indexes
    1745
     46def _table_info(cursor, name):
     47    cursor.execute('PRAGMA table_info(%s)' % quote_name(name))
     48    # cid, name, type, notnull, dflt_value, pk
     49    return [{'name': field[1],
     50             'type': field[2],
     51             'null_ok': not field[3],
     52             'pk': field[5]     # undocumented
     53             } for field in cursor.fetchall()]
     54
    1855# Maps SQL types to Django Field types. Some of the SQL types have multiple
    1956# entries here because SQLite allows for anything and doesn't normalize the
    2057# field type; it uses whatever was given.
  • docs/django-admin.txt

     
    126126customizations. In particular, you'll need to rearrange models' order, so that
    127127models that refer to other models are ordered properly.
    128128
    129 Primary keys are automatically introspected for PostgreSQL and MySQL, in which
    130 case Django puts in the ``primary_key=True`` where needed.
     129Primary keys are automatically introspected for PostgreSQL, MySQL and
     130SQLite, in which case Django puts in the ``primary_key=True`` where
     131needed.
    131132
    132133``inspectdb`` works with PostgreSQL, MySQL and SQLite. Foreign-key detection
    133134only works in PostgreSQL and with certain types of MySQL tables.
Back to Top