from django.db import models, connection
from django.core.management.color import no_style

from multi_schema.models import Schema

def post_syncdb_duplicator(sender, **kwargs):
    # See if any of the newly created models are schema-aware
    schema_aware_models = [m for m in kwargs['created_models'] if m._is_schema_aware and kwargs['app'].__name__ == m.__module__]
    if schema_aware_models:
        import pdb; pdb.set_trace()
        for schema in Schema.objects.all():
            cursor = connection.cursor()
            tables = connection.introspection.table_names()
            known_models = set([model for model in connection.introspection.installed_models(tables)])
            for model in schema_aware_models:
                output, references = connection.creation.sql_create_model(model, no_style(), known_models, schema.schema)
                if output and kwargs.get('verbosity', 0) > 1:
                    print "Creating table %s in schema %s" % (model._meta.db_table, schema.schema)
                for statement in output:
                    cursor.execute(statement)

models.signals.post_syncdb.connect(post_syncdb_duplicator)
@@ -394,44 +394,8 @@
 ls:%0A
-        import pdb; pdb.set_trace()%0A
     
@@ -475,16 +475,52 @@
 ursor()%0A
+            schema.activate(cursor)%0A
         
@@ -571,16 +571,52 @@
 names()%0A
+            pending_references = %7B%7D%0A
         
@@ -710,16 +710,60 @@
 bles)%5D)%0A
+            seen_models = set(known_models)%0A
         
@@ -914,32 +914,504 @@
  schema.schema)%0A
+                seen_models.add(model)%0A                for refto, refs in references.items():%0A                    pending_references.setdefault(refto, %5B%5D).extend(refs)%0A                    if refto in seen_models:%0A                        output.extend(connection.creation.sql_for_pending_references(refto, no_style(), pending_references))%0A                output.extend(connection.creation.sql_for_pending_references(model, no_style(), pending_references))%0A                %0A
                 
@@ -1487,9 +1487,14 @@
 nt %22
-C
+ ... c
 reat
@@ -1646,16 +1646,132 @@
 atement)
+%0A                tables.append(connection.introspection.table_name_converter(model._meta.db_table))%0A                
 %0A%0Amodels

Create the correct references to other tables in all schemata.