mirror of
				https://github.com/django/django.git
				synced 2025-10-30 17:16:10 +00:00 
			
		
		
		
	magic-removal: Added a couple of comments to d.c.management.get_sql_create
git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@1707 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
		| @@ -62,10 +62,12 @@ get_rel_data_type = lambda f: (f.get_internal_type() == 'AutoField') and 'Intege | |||||||
| def get_sql_create(mod): | def get_sql_create(mod): | ||||||
|     "Returns a list of the CREATE TABLE SQL statements for the given module." |     "Returns a list of the CREATE TABLE SQL statements for the given module." | ||||||
|     from django.db import backend, get_creation_module, models |     from django.db import backend, get_creation_module, models | ||||||
|  |  | ||||||
|     data_types = get_creation_module().DATA_TYPES |     data_types = get_creation_module().DATA_TYPES | ||||||
|     final_output = [] |     final_output = [] | ||||||
|     opts_output = set() |     opts_output = set() | ||||||
|     pending_references = {} |     pending_references = {} | ||||||
|  |  | ||||||
|     for klass in mod._MODELS: |     for klass in mod._MODELS: | ||||||
|         opts = klass._meta |         opts = klass._meta | ||||||
|         table_output = [] |         table_output = [] | ||||||
| @@ -78,6 +80,7 @@ def get_sql_create(mod): | |||||||
|                 data_type = f.get_internal_type() |                 data_type = f.get_internal_type() | ||||||
|             col_type = data_types[data_type] |             col_type = data_types[data_type] | ||||||
|             if col_type is not None: |             if col_type is not None: | ||||||
|  |                 # Make the definition (e.g. 'foo VARCHAR(30)') for this field. | ||||||
|                 field_output = [backend.quote_name(f.column), col_type % rel_field.__dict__] |                 field_output = [backend.quote_name(f.column), col_type % rel_field.__dict__] | ||||||
|                 field_output.append('%sNULL' % (not f.null and 'NOT ' or '')) |                 field_output.append('%sNULL' % (not f.null and 'NOT ' or '')) | ||||||
|                 if f.unique: |                 if f.unique: | ||||||
| @@ -90,7 +93,9 @@ def get_sql_create(mod): | |||||||
|                              (backend.quote_name(f.rel.to._meta.db_table), |                              (backend.quote_name(f.rel.to._meta.db_table), | ||||||
|                              backend.quote_name(f.rel.to._meta.get_field(f.rel.field_name).column))) |                              backend.quote_name(f.rel.to._meta.get_field(f.rel.field_name).column))) | ||||||
|                      else: |                      else: | ||||||
|                          pr = pending_references.setdefault(f.rel.to._meta, []).append( (opts, f) ) |                          # We haven't yet created the table to which this field | ||||||
|  |                          # is related, so save it for later. | ||||||
|  |                          pr = pending_references.setdefault(f.rel.to._meta, []).append((opts, f)) | ||||||
|                 table_output.append(' '.join(field_output)) |                 table_output.append(' '.join(field_output)) | ||||||
|         if opts.order_with_respect_to: |         if opts.order_with_respect_to: | ||||||
|             table_output.append('%s %s NULL' % (backend.quote_name('_order'), data_types['IntegerField'])) |             table_output.append('%s %s NULL' % (backend.quote_name('_order'), data_types['IntegerField'])) | ||||||
| @@ -103,20 +108,25 @@ def get_sql_create(mod): | |||||||
|             full_statement.append('    %s%s' % (line, i < len(table_output)-1 and ',' or '')) |             full_statement.append('    %s%s' % (line, i < len(table_output)-1 and ',' or '')) | ||||||
|         full_statement.append(');') |         full_statement.append(');') | ||||||
|         final_output.append('\n'.join(full_statement)) |         final_output.append('\n'.join(full_statement)) | ||||||
|  |  | ||||||
|  |         # Take care of any ALTER TABLE statements to add constraints | ||||||
|  |         # after the fact. | ||||||
|         if opts in pending_references: |         if opts in pending_references: | ||||||
|             for (rel_opts, f) in pending_references[opts]: |             for rel_opts, f in pending_references[opts]: | ||||||
|                 r_table = rel_opts.db_table |                 r_table = rel_opts.db_table | ||||||
|                 r_col = f.column |                 r_col = f.column | ||||||
|                 table =  opts.db_table |                 table = opts.db_table | ||||||
|                 col = opts.get_field(f.rel.field_name).column |                 col = opts.get_field(f.rel.field_name).column | ||||||
|                 final_output.append( 'ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s);'  % \ |                 final_output.append('ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s);' % \ | ||||||
|                                      (backend.quote_name(r_table), |                     (backend.quote_name(r_table), | ||||||
|                                       backend.quote_name("%s_referencing_%s_%s" % (r_col,table,col)), |                     backend.quote_name("%s_referencing_%s_%s" % (r_col, table, col)), | ||||||
|                                       backend.quote_name(r_col), backend.quote_name(table),backend.quote_name(col)) |                     backend.quote_name(r_col), backend.quote_name(table), backend.quote_name(col))) | ||||||
|                                      ) |  | ||||||
|             del pending_references[opts] |             del pending_references[opts] | ||||||
|  |  | ||||||
|  |         # Keep track of the fact that we've created the table for this model. | ||||||
|         opts_output.add(opts) |         opts_output.add(opts) | ||||||
|  |  | ||||||
|  |     # Create the many-to-many join tables. | ||||||
|     for klass in mod._MODELS: |     for klass in mod._MODELS: | ||||||
|         opts = klass._meta |         opts = klass._meta | ||||||
|         for f in opts.many_to_many: |         for f in opts.many_to_many: | ||||||
| @@ -205,7 +215,7 @@ def get_sql_delete(mod): | |||||||
|                      r_col = f.rel.to.get_field(f.rel.field_name).column |                      r_col = f.rel.to.get_field(f.rel.field_name).column | ||||||
|                      output.append('ALTER TABLE %s DROP CONSTRAINT %s;' % \ |                      output.append('ALTER TABLE %s DROP CONSTRAINT %s;' % \ | ||||||
|                         (backend.quote_name(table), |                         (backend.quote_name(table), | ||||||
|                          backend.quote_name("%s_referencing_%s_%s" % (col,r_table,r_col)))) |                          backend.quote_name("%s_referencing_%s_%s" % (col, r_table, r_col)))) | ||||||
|  |  | ||||||
|     # Output DROP TABLE statements for many-to-many tables. |     # Output DROP TABLE statements for many-to-many tables. | ||||||
|     for klass in mod._MODELS: |     for klass in mod._MODELS: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user