Index: P:/org/django/django/core/management.py
===================================================================
--- P:/org/django/django/core/management.py	(revision 2997)
+++ P:/org/django/django/core/management.py	(working copy)
@@ -128,6 +128,138 @@
 get_sql_create.help_doc = "Prints the CREATE TABLE SQL statements for the given app name(s)."
 get_sql_create.args = APP_ARGS
 
+#------------------------------------------------------------------------------
+
+def table_field_exists(klass, f):
+    """
+    Checks if a field is available within a table
+    
+    Limited implementation, verifies only field-name
+
+    Status: draft
+    """    
+    from django.db import backend, connection
+
+    cursor = connection.cursor()
+    backend_column_name = f.column
+
+    cursor.execute('select * from %s where 1 = 0' % klass._meta.db_table )
+    
+    exist = False
+    for col in cursor.description:
+        if backend_column_name == col[0]:
+            exist = True
+            break
+            
+    return exist        
+
+#------------------------------------------------------------------------------
+def _get_sql_table_evolve(klass):
+    """
+    Get the SQL required to evolve a single model.
+
+    Uses ALTER TABLE
+    
+    Status: draft, sqlite3 specific.
+    """
+    from django.db import backend
+       
+    final_output = []
+    str = ''
+    for f in klass._meta.fields:                    
+        if not table_field_exists(klass, f):
+            statement, ref = _get_sql_field_def(klass, f)
+            field_def = ' '.join(statement)
+                   
+            full_statement = [style.SQL_KEYWORD('ALTER TABLE') + ' %s ADD COLUMN %s;' % \
+                ( style.SQL_TABLE(backend.quote_name( klass._meta.db_table )), field_def) ]
+            final_output.append('\n'.join(full_statement))
+
+    if final_output:
+        full_statement = [style.SQL_KEYWORD('VACUUM') + ';']
+        final_output.append('\n'.join(full_statement))          
+                      
+    return final_output
+
+#------------------------------------------------------------------------------
+
+def table_evolve(klass):
+    """
+    evolves the underlying table for a single model
+    
+    Status: draft
+    """
+    from django.db import backend, connection, transaction
+
+    print "Checking table  %s model" % klass.__name__
+
+    cursor = connection.cursor()
+    statements = _get_sql_table_evolve(klass)
+    
+    if statements:
+        print "Evolving table for Class %s" % klass.__name__
+        for statement in statements:
+            cursor.execute(statement)
+
+        transaction.commit_unless_managed()
+        
+#------------------------------------------------------------------------------
+        
+def _get_sql_field_def(klass, f, models_already_seen=set() ):
+    """
+    Get the SQL for a field definition
+    """
+    
+    # copied from _get_sql_model_create
+    # not neede lines are outcommented (will be removed after review)
+
+    from django.db import backend, get_creation_module, models
+    data_types = get_creation_module().DATA_TYPES
+
+    #opts = klass._meta
+    #final_output = []
+    #table_output = []
+    #pending_references = {}
+
+    if isinstance(f, models.ForeignKey):
+        rel_field = f.rel.get_related_field()
+        data_type = get_rel_data_type(rel_field)
+    else:
+        rel_field = f
+        data_type = f.get_internal_type()
+    col_type = data_types[data_type]
+    if col_type is not None:
+        # Make the definition (e.g. 'foo VARCHAR(30)') for this field.
+        field_output = [style.SQL_FIELD(backend.quote_name(f.column)),
+            style.SQL_COLTYPE(col_type % rel_field.__dict__)]
+        field_output.append(style.SQL_KEYWORD('%sNULL' % (not f.null and 'NOT ' or '')))
+        if f.unique:
+            field_output.append(style.SQL_KEYWORD('UNIQUE'))
+        if f.primary_key:
+            field_output.append(style.SQL_KEYWORD('PRIMARY KEY'))
+        if f.rel:
+            if f.rel.to in models_already_seen:
+                field_output.append(style.SQL_KEYWORD('REFERENCES') + ' ' + \
+                    style.SQL_TABLE(backend.quote_name(f.rel.to._meta.db_table)) + ' (' + \
+                    style.SQL_FIELD(backend.quote_name(f.rel.to._meta.get_field(f.rel.field_name).column)) + ')'
+                )
+            #else:
+                # 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, []).append((klass, f))
+        #table_output.append(' '.join(field_output))
+        
+        # code added during refactoring
+        has_pending_ref = False
+        if f.rel:
+            if f.rel.to not in models_already_seen:
+                has_pending_ref = True
+                    
+        
+    return field_output, has_pending_ref
+
+#------------------------------------------------------------------------------
+
 def _get_sql_model_create(klass, models_already_seen=set()):
     """
     Get the SQL required to create a single model.
@@ -141,34 +273,14 @@
     final_output = []
     table_output = []
     pending_references = {}
-    for f in opts.fields:
-        if isinstance(f, models.ForeignKey):
-            rel_field = f.rel.get_related_field()
-            data_type = get_rel_data_type(rel_field)
-        else:
-            rel_field = f
-            data_type = f.get_internal_type()
-        col_type = data_types[data_type]
-        if col_type is not None:
-            # Make the definition (e.g. 'foo VARCHAR(30)') for this field.
-            field_output = [style.SQL_FIELD(backend.quote_name(f.column)),
-                style.SQL_COLTYPE(col_type % rel_field.__dict__)]
-            field_output.append(style.SQL_KEYWORD('%sNULL' % (not f.null and 'NOT ' or '')))
-            if f.unique:
-                field_output.append(style.SQL_KEYWORD('UNIQUE'))
-            if f.primary_key:
-                field_output.append(style.SQL_KEYWORD('PRIMARY KEY'))
-            if f.rel:
-                if f.rel.to in models_already_seen:
-                    field_output.append(style.SQL_KEYWORD('REFERENCES') + ' ' + \
-                        style.SQL_TABLE(backend.quote_name(f.rel.to._meta.db_table)) + ' (' + \
-                        style.SQL_FIELD(backend.quote_name(f.rel.to._meta.get_field(f.rel.field_name).column)) + ')'
-                    )
-                else:
-                    # 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, []).append((klass, f))
-            table_output.append(' '.join(field_output))
+    
+    for f in opts.fields:       
+        field_output, has_pending_ref = _get_sql_field_def(klass, f, models_already_seen)
+        table_output.append(' '.join(field_output))
+        
+        if has_pending_ref:
+            pr = pending_references.setdefault(f.rel.to, []).append((klass, f))
+                    
     if opts.order_with_respect_to:
         table_output.append(style.SQL_FIELD(backend.quote_name('_order')) + ' ' + \
             style.SQL_COLTYPE(data_types['IntegerField']) + ' ' + \
@@ -455,6 +567,7 @@
         for model in model_list:
             # Create the model's database table, if it doesn't already exist.
             if model._meta.db_table in table_list:
+                table_evolve(model)                
                 continue
             sql, references = _get_sql_model_create(model, seen_models)
             seen_models.add(model)
