Ticket #2272: django-sqlite-introspection.diff
File django-sqlite-introspection.diff, 3.6 KB (added by , 18 years ago) |
---|
-
django/db/backends/sqlite3/introspection.py
2 2 from django.db.backends.sqlite3.base import quote_name 3 3 4 4 def 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""") 6 11 return [row[0] for row in cursor.fetchall()] 7 12 8 13 def 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)] 11 17 12 18 def get_relations(cursor, table_name): 13 19 raise NotImplementedError 14 20 15 21 def 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 17 45 46 def _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 18 55 # Maps SQL types to Django Field types. Some of the SQL types have multiple 19 56 # entries here because SQLite allows for anything and doesn't normalize the 20 57 # field type; it uses whatever was given. -
docs/django-admin.txt
126 126 customizations. In particular, you'll need to rearrange models' order, so that 127 127 models that refer to other models are ordered properly. 128 128 129 Primary keys are automatically introspected for PostgreSQL and MySQL, in which 130 case Django puts in the ``primary_key=True`` where needed. 129 Primary keys are automatically introspected for PostgreSQL, MySQL and 130 SQLite, in which case Django puts in the ``primary_key=True`` where 131 needed. 131 132 132 133 ``inspectdb`` works with PostgreSQL, MySQL and SQLite. Foreign-key detection 133 134 only works in PostgreSQL and with certain types of MySQL tables.