You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

inspectdb.py 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import keyword
  2. import re
  3. from collections import OrderedDict
  4. from django.core.management.base import BaseCommand, CommandError
  5. from django.db import DEFAULT_DB_ALIAS, connections
  6. from django.db.models.constants import LOOKUP_SEP
  7. class Command(BaseCommand):
  8. help = "Introspects the database tables in the given database and outputs a Django model module."
  9. requires_system_checks = False
  10. stealth_options = ('table_name_filter',)
  11. db_module = 'django.db'
  12. def add_arguments(self, parser):
  13. parser.add_argument(
  14. 'table', nargs='*', type=str,
  15. help='Selects what tables or views should be introspected.',
  16. )
  17. parser.add_argument(
  18. '--database', default=DEFAULT_DB_ALIAS,
  19. help='Nominates a database to introspect. Defaults to using the "default" database.',
  20. )
  21. parser.add_argument(
  22. '--include-partitions', action='store_true', help='Also output models for partition tables.',
  23. )
  24. parser.add_argument(
  25. '--include-views', action='store_true', help='Also output models for database views.',
  26. )
  27. def handle(self, **options):
  28. try:
  29. for line in self.handle_inspection(options):
  30. self.stdout.write("%s\n" % line)
  31. except NotImplementedError:
  32. raise CommandError("Database inspection isn't supported for the currently selected database backend.")
  33. def handle_inspection(self, options):
  34. connection = connections[options['database']]
  35. # 'table_name_filter' is a stealth option
  36. table_name_filter = options.get('table_name_filter')
  37. def table2model(table_name):
  38. return re.sub(r'[^a-zA-Z0-9]', '', table_name.title())
  39. with connection.cursor() as cursor:
  40. yield "# This is an auto-generated Django model module."
  41. yield "# You'll have to do the following manually to clean this up:"
  42. yield "# * Rearrange models' order"
  43. yield "# * Make sure each model has one field with primary_key=True"
  44. yield "# * Make sure each ForeignKey has `on_delete` set to the desired behavior."
  45. yield (
  46. "# * Remove `managed = False` lines if you wish to allow "
  47. "Django to create, modify, and delete the table"
  48. )
  49. yield "# Feel free to rename the models, but don't rename db_table values or field names."
  50. yield 'from %s import models' % self.db_module
  51. known_models = []
  52. table_info = connection.introspection.get_table_list(cursor)
  53. # Determine types of tables and/or views to be introspected.
  54. types = {'t'}
  55. if options['include_partitions']:
  56. types.add('p')
  57. if options['include_views']:
  58. types.add('v')
  59. for table_name in (options['table'] or sorted(info.name for info in table_info if info.type in types)):
  60. if table_name_filter is not None and callable(table_name_filter):
  61. if not table_name_filter(table_name):
  62. continue
  63. try:
  64. try:
  65. relations = connection.introspection.get_relations(cursor, table_name)
  66. except NotImplementedError:
  67. relations = {}
  68. try:
  69. constraints = connection.introspection.get_constraints(cursor, table_name)
  70. except NotImplementedError:
  71. constraints = {}
  72. primary_key_column = connection.introspection.get_primary_key_column(cursor, table_name)
  73. unique_columns = [
  74. c['columns'][0] for c in constraints.values()
  75. if c['unique'] and len(c['columns']) == 1
  76. ]
  77. table_description = connection.introspection.get_table_description(cursor, table_name)
  78. except Exception as e:
  79. yield "# Unable to inspect table '%s'" % table_name
  80. yield "# The error was: %s" % e
  81. continue
  82. yield ''
  83. yield ''
  84. yield 'class %s(models.Model):' % table2model(table_name)
  85. known_models.append(table2model(table_name))
  86. used_column_names = [] # Holds column names used in the table so far
  87. column_to_field_name = {} # Maps column names to names of model fields
  88. for row in table_description:
  89. comment_notes = [] # Holds Field notes, to be displayed in a Python comment.
  90. extra_params = OrderedDict() # Holds Field parameters such as 'db_column'.
  91. column_name = row.name
  92. is_relation = column_name in relations
  93. att_name, params, notes = self.normalize_col_name(
  94. column_name, used_column_names, is_relation)
  95. extra_params.update(params)
  96. comment_notes.extend(notes)
  97. used_column_names.append(att_name)
  98. column_to_field_name[column_name] = att_name
  99. # Add primary_key and unique, if necessary.
  100. if column_name == primary_key_column:
  101. extra_params['primary_key'] = True
  102. elif column_name in unique_columns:
  103. extra_params['unique'] = True
  104. if is_relation:
  105. rel_to = (
  106. "self" if relations[column_name][1] == table_name
  107. else table2model(relations[column_name][1])
  108. )
  109. if rel_to in known_models:
  110. field_type = 'ForeignKey(%s' % rel_to
  111. else:
  112. field_type = "ForeignKey('%s'" % rel_to
  113. else:
  114. # Calling `get_field_type` to get the field type string and any
  115. # additional parameters and notes.
  116. field_type, field_params, field_notes = self.get_field_type(connection, table_name, row)
  117. extra_params.update(field_params)
  118. comment_notes.extend(field_notes)
  119. field_type += '('
  120. # Don't output 'id = meta.AutoField(primary_key=True)', because
  121. # that's assumed if it doesn't exist.
  122. if att_name == 'id' and extra_params == {'primary_key': True}:
  123. if field_type == 'AutoField(':
  124. continue
  125. elif field_type == 'IntegerField(' and not connection.features.can_introspect_autofield:
  126. comment_notes.append('AutoField?')
  127. # Add 'null' and 'blank', if the 'null_ok' flag was present in the
  128. # table description.
  129. if row.null_ok: # If it's NULL...
  130. extra_params['blank'] = True
  131. extra_params['null'] = True
  132. field_desc = '%s = %s%s' % (
  133. att_name,
  134. # Custom fields will have a dotted path
  135. '' if '.' in field_type else 'models.',
  136. field_type,
  137. )
  138. if field_type.startswith('ForeignKey('):
  139. field_desc += ', models.DO_NOTHING'
  140. if extra_params:
  141. if not field_desc.endswith('('):
  142. field_desc += ', '
  143. field_desc += ', '.join('%s=%r' % (k, v) for k, v in extra_params.items())
  144. field_desc += ')'
  145. if comment_notes:
  146. field_desc += ' # ' + ' '.join(comment_notes)
  147. yield ' %s' % field_desc
  148. is_view = any(info.name == table_name and info.type == 'v' for info in table_info)
  149. is_partition = any(info.name == table_name and info.type == 'p' for info in table_info)
  150. for meta_line in self.get_meta(table_name, constraints, column_to_field_name, is_view, is_partition):
  151. yield meta_line
  152. def normalize_col_name(self, col_name, used_column_names, is_relation):
  153. """
  154. Modify the column name to make it Python-compatible as a field name
  155. """
  156. field_params = {}
  157. field_notes = []
  158. new_name = col_name.lower()
  159. if new_name != col_name:
  160. field_notes.append('Field name made lowercase.')
  161. if is_relation:
  162. if new_name.endswith('_id'):
  163. new_name = new_name[:-3]
  164. else:
  165. field_params['db_column'] = col_name
  166. new_name, num_repl = re.subn(r'\W', '_', new_name)
  167. if num_repl > 0:
  168. field_notes.append('Field renamed to remove unsuitable characters.')
  169. if new_name.find(LOOKUP_SEP) >= 0:
  170. while new_name.find(LOOKUP_SEP) >= 0:
  171. new_name = new_name.replace(LOOKUP_SEP, '_')
  172. if col_name.lower().find(LOOKUP_SEP) >= 0:
  173. # Only add the comment if the double underscore was in the original name
  174. field_notes.append("Field renamed because it contained more than one '_' in a row.")
  175. if new_name.startswith('_'):
  176. new_name = 'field%s' % new_name
  177. field_notes.append("Field renamed because it started with '_'.")
  178. if new_name.endswith('_'):
  179. new_name = '%sfield' % new_name
  180. field_notes.append("Field renamed because it ended with '_'.")
  181. if keyword.iskeyword(new_name):
  182. new_name += '_field'
  183. field_notes.append('Field renamed because it was a Python reserved word.')
  184. if new_name[0].isdigit():
  185. new_name = 'number_%s' % new_name
  186. field_notes.append("Field renamed because it wasn't a valid Python identifier.")
  187. if new_name in used_column_names:
  188. num = 0
  189. while '%s_%d' % (new_name, num) in used_column_names:
  190. num += 1
  191. new_name = '%s_%d' % (new_name, num)
  192. field_notes.append('Field renamed because of name conflict.')
  193. if col_name != new_name and field_notes:
  194. field_params['db_column'] = col_name
  195. return new_name, field_params, field_notes
  196. def get_field_type(self, connection, table_name, row):
  197. """
  198. Given the database connection, the table name, and the cursor row
  199. description, this routine will return the given field type name, as
  200. well as any additional keyword parameters and notes for the field.
  201. """
  202. field_params = OrderedDict()
  203. field_notes = []
  204. try:
  205. field_type = connection.introspection.get_field_type(row.type_code, row)
  206. except KeyError:
  207. field_type = 'TextField'
  208. field_notes.append('This field type is a guess.')
  209. # This is a hook for data_types_reverse to return a tuple of
  210. # (field_type, field_params_dict).
  211. if type(field_type) is tuple:
  212. field_type, new_params = field_type
  213. field_params.update(new_params)
  214. # Add max_length for all CharFields.
  215. if field_type == 'CharField' and row.internal_size:
  216. field_params['max_length'] = int(row.internal_size)
  217. if field_type == 'DecimalField':
  218. if row.precision is None or row.scale is None:
  219. field_notes.append(
  220. 'max_digits and decimal_places have been guessed, as this '
  221. 'database handles decimal fields as float')
  222. field_params['max_digits'] = row.precision if row.precision is not None else 10
  223. field_params['decimal_places'] = row.scale if row.scale is not None else 5
  224. else:
  225. field_params['max_digits'] = row.precision
  226. field_params['decimal_places'] = row.scale
  227. return field_type, field_params, field_notes
  228. def get_meta(self, table_name, constraints, column_to_field_name, is_view, is_partition):
  229. """
  230. Return a sequence comprising the lines of code necessary
  231. to construct the inner Meta class for the model corresponding
  232. to the given database table name.
  233. """
  234. unique_together = []
  235. has_unsupported_constraint = False
  236. for params in constraints.values():
  237. if params['unique']:
  238. columns = params['columns']
  239. if None in columns:
  240. has_unsupported_constraint = True
  241. columns = [x for x in columns if x is not None]
  242. if len(columns) > 1:
  243. unique_together.append(str(tuple(column_to_field_name[c] for c in columns)))
  244. if is_view:
  245. managed_comment = " # Created from a view. Don't remove."
  246. elif is_partition:
  247. managed_comment = " # Created from a partition. Don't remove."
  248. else:
  249. managed_comment = ''
  250. meta = ['']
  251. if has_unsupported_constraint:
  252. meta.append(' # A unique constraint could not be introspected.')
  253. meta += [
  254. ' class Meta:',
  255. ' managed = False%s' % managed_comment,
  256. ' db_table = %r' % table_name
  257. ]
  258. if unique_together:
  259. tup = '(' + ', '.join(unique_together) + ',)'
  260. meta += [" unique_together = %s" % tup]
  261. return meta