Changeset 2351
- Timestamp:
- 02/18/06 16:14:05 (3 years ago)
- Files:
-
- django/branches/magic-removal/AUTHORS (modified) (1 diff)
- django/branches/magic-removal/django/core/management.py (modified) (4 diffs)
- django/branches/magic-removal/django/db/backends/ado_mssql/introspection.py (modified) (1 diff)
- django/branches/magic-removal/django/db/backends/dummy/introspection.py (modified) (1 diff)
- django/branches/magic-removal/django/db/backends/mysql/introspection.py (modified) (1 diff)
- django/branches/magic-removal/django/db/backends/postgresql/introspection.py (modified) (1 diff)
- django/branches/magic-removal/django/db/backends/sqlite3/introspection.py (modified) (1 diff)
- django/branches/magic-removal/docs/django-admin.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/magic-removal/AUTHORS
r2344 r2351 52 52 Jeremy Dunck <http://dunck.us/> 53 53 Clint Ecker 54 gandalf@owca.info 54 55 Baishampayan Ghose 55 56 Espen Grindhaug <http://grindhaug.org/> django/branches/magic-removal/django/core/management.py
r2330 r2351 753 753 yield "# You'll have to do the following manually to clean this up:" 754 754 yield "# * Rearrange models' order" 755 yield "# * Add primary_key=True to one field in each model."755 yield "# * Make sure each model has one field with primary_key=True" 756 756 yield "# Feel free to rename the models, but don't rename db_table values or field names." 757 757 yield "#" … … 767 767 except NotImplementedError: 768 768 relations = {} 769 try: 770 indexes = introspection_module.get_indexes(cursor, table_name) 771 except NotImplementedError: 772 indexes = {} 769 773 for i, row in enumerate(introspection_module.get_table_description(cursor, table_name)): 770 column_name = row[0]774 att_name = row[0] 771 775 comment_notes = [] # Holds Field notes, to be displayed in a Python comment. 772 776 extra_params = {} # Holds Field parameters such as 'db_column'. 773 777 774 if keyword.iskeyword( column_name):775 extra_params['db_column'] = column_name776 column_name += '_field'778 if keyword.iskeyword(att_name): 779 extra_params['db_column'] = att_name 780 att_name += '_field' 777 781 comment_notes.append('Field renamed because it was a Python reserved word.') 778 782 … … 780 784 rel_to = relations[i][1] == table_name and "'self'" or table2model(relations[i][1]) 781 785 field_type = 'ForeignKey(%s' % rel_to 782 if column_name.endswith('_id'):783 column_name = column_name[:-3]786 if att_name.endswith('_id'): 787 att_name = att_name[:-3] 784 788 else: 785 extra_params['db_column'] = column_name789 extra_params['db_column'] = att_name 786 790 else: 787 791 try: … … 798 802 extra_params.update(new_params) 799 803 804 # Add maxlength for all CharFields. 800 805 if field_type == 'CharField' and row[3]: 801 806 extra_params['maxlength'] = row[3] 802 807 808 # Add primary_key and unique, if necessary. 809 column_name = extra_params.get('db_column', att_name) 810 if column_name in indexes: 811 if indexes[column_name]['primary_key']: 812 extra_params['primary_key'] = True 813 elif indexes[column_name]['unique']: 814 extra_params['unique'] = True 815 803 816 field_type += '(' 804 817 805 806 field_desc = '%s = models.%s' % (column_name, field_type) 818 # Don't output 'id = meta.AutoField(primary_key=True)', because 819 # that's assumed if it doesn't exist. 820 if att_name == 'id' and field_type == 'AutoField(' and extra_params == {'primary_key': True}: 821 continue 822 823 field_desc = '%s = models.%s' % (att_name, field_type) 807 824 if extra_params: 808 825 if not field_desc.endswith('('): django/branches/magic-removal/django/db/backends/ado_mssql/introspection.py
r1632 r2351 8 8 raise NotImplementedError 9 9 10 def get_indexes(cursor, table_name): 11 raise NotImplementedError 12 10 13 DATA_TYPES_REVERSE = {} django/branches/magic-removal/django/db/backends/dummy/introspection.py
r1803 r2351 4 4 get_table_description = complain 5 5 get_relations = complain 6 get_indexes = complain 6 7 7 8 DATA_TYPES_REVERSE = {} django/branches/magic-removal/django/db/backends/mysql/introspection.py
r1689 r2351 14 14 def get_relations(cursor, table_name): 15 15 raise NotImplementedError 16 17 def get_indexes(cursor, table_name): 18 """ 19 Returns a dictionary of fieldname -> infodict for the given table, 20 where each infodict is in the format: 21 {'primary_key': boolean representing whether it's the primary key, 22 'unique': boolean representing whether it's a unique index} 23 """ 24 cursor.execute("SHOW INDEX FROM %s" % quote_name(table_name)) 25 indexes = {} 26 for row in cursor.fetchall(): 27 indexes[row[4]] = {'primary_key': (row[2] == 'PRIMARY'), 'unique': not bool(row[1])} 28 return indexes 16 29 17 30 DATA_TYPES_REVERSE = { django/branches/magic-removal/django/db/backends/postgresql/introspection.py
r1689 r2351 38 38 return relations 39 39 40 def get_indexes(cursor, table_name): 41 """ 42 Returns a dictionary of fieldname -> infodict for the given table, 43 where each infodict is in the format: 44 {'primary_key': boolean representing whether it's the primary key, 45 'unique': boolean representing whether it's a unique index} 46 """ 47 # Get the table description because we only have the column indexes, and we 48 # need the column names. 49 desc = get_table_description(cursor, table_name) 50 # This query retrieves each index on the given table. 51 cursor.execute(""" 52 SELECT idx.indkey, idx.indisunique, idx.indisprimary 53 FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, 54 pg_catalog.pg_index idx 55 WHERE c.oid = idx.indrelid 56 AND idx.indexrelid = c2.oid 57 AND c.relname = %s""", [table_name]) 58 indexes = {} 59 for row in cursor.fetchall(): 60 # row[0] (idx.indkey) is stored in the DB as an array. It comes out as 61 # a string of space-separated integers. This designates the field 62 # indexes (1-based) of the fields that have indexes on the table. 63 # Here, we skip any indexes across multiple fields. 64 if ' ' in row[0]: 65 continue 66 col_name = desc[int(row[0])-1][0] 67 indexes[col_name] = {'primary_key': row[2], 'unique': row[1]} 68 return indexes 69 40 70 # Maps type codes to Django Field types. 41 71 DATA_TYPES_REVERSE = { django/branches/magic-removal/django/db/backends/sqlite3/introspection.py
r2273 r2351 10 10 11 11 def get_relations(cursor, table_name): 12 raise NotImplementedError 13 14 def get_indexes(cursor, table_name): 12 15 raise NotImplementedError 13 16 django/branches/magic-removal/docs/django-admin.txt
r2273 r2351 113 113 This feature is meant as a shortcut, not as definitive model generation. After 114 114 you run it, you'll want to look over the generated models yourself to make 115 customizations. In particular, you'll need to do this: 116 117 * Rearrange models' order, so that models that refer to other models are 118 ordered properly. 119 * Add ``primary_key=True`` to one field in each model. The ``inspectdb`` 120 doesn't yet introspect primary keys. 115 customizations. In particular, you'll need to rearrange models' order, so that 116 models that refer to other models are ordered properly. 117 118 If you're using Django 0.90 or 0.91, you'll need to add ``primary_key=True`` to 119 one field in each model. In the Django development version, primary keys are 120 automatically introspected for PostgreSQL and MySQL, and Django puts in the 121 ``primary_key=True`` where needed. 121 122 122 123 ``inspectdb`` works with PostgreSQL, MySQL and SQLite. Foreign-key detection
