Ticket #2040: _get_sql_field_def.diff

File _get_sql_field_def.diff, 2.8 KB (added by ilias lazaridis <ilias@…>, 18 years ago)
  • P:/org/django/django/core/management.py

     
    128128get_sql_create.help_doc = "Prints the CREATE TABLE SQL statements for the given app name(s)."
    129129get_sql_create.args = APP_ARGS
    130130
     131#------------------------------------------------------------------------------
     132
     133def _get_sql_field_def(klass, f, models_already_seen=set() ):
     134    """
     135    Get the SQL for a field definition
     136    """
     137   
     138    # copied from _get_sql_model_create
     139    # not neede lines are outcommented (will be removed after review)
     140
     141    from django.db import backend, get_creation_module, models
     142    data_types = get_creation_module().DATA_TYPES
     143
     144    #opts = klass._meta
     145    #final_output = []
     146    #table_output = []
     147    #pending_references = {}
     148
     149    if isinstance(f, models.ForeignKey):
     150        rel_field = f.rel.get_related_field()
     151        data_type = get_rel_data_type(rel_field)
     152    else:
     153        rel_field = f
     154        data_type = f.get_internal_type()
     155    col_type = data_types[data_type]
     156    if col_type is not None:
     157        # Make the definition (e.g. 'foo VARCHAR(30)') for this field.
     158        field_output = [style.SQL_FIELD(backend.quote_name(f.column)),
     159            style.SQL_COLTYPE(col_type % rel_field.__dict__)]
     160        field_output.append(style.SQL_KEYWORD('%sNULL' % (not f.null and 'NOT ' or '')))
     161        if f.unique:
     162            field_output.append(style.SQL_KEYWORD('UNIQUE'))
     163        if f.primary_key:
     164            field_output.append(style.SQL_KEYWORD('PRIMARY KEY'))
     165        if f.rel:
     166            if f.rel.to in models_already_seen:
     167                field_output.append(style.SQL_KEYWORD('REFERENCES') + ' ' + \
     168                    style.SQL_TABLE(backend.quote_name(f.rel.to._meta.db_table)) + ' (' + \
     169                    style.SQL_FIELD(backend.quote_name(f.rel.to._meta.get_field(f.rel.field_name).column)) + ')'
     170                )
     171            #else:
     172                # We haven't yet created the table to which this field
     173                # is related, so save it for later.
     174                #pr = pending_references.setdefault(f.rel.to, []).append((klass, f))
     175        #table_output.append(' '.join(field_output))
     176       
     177        # code added during refactoring
     178        has_pending_ref = False
     179        if f.rel:
     180            if f.rel.to not in models_already_seen:
     181                has_pending_ref = True
     182                   
     183       
     184    return field_output, has_pending_ref
     185
     186#------------------------------------------------------------------------------
     187
    131188def _get_sql_model_create(klass, models_already_seen=set()):
    132189    """
    133190    Get the SQL required to create a single model.
Back to Top