Hey Oleg,
I encountered some annoying bugs in sqlobject-admin and figured I would pass 
on the patch that fixes them.

1. on a "sqlobject-admin create" where two tables reference each other, the 
constraints where not being deferred to after the tables were created, thus 
errors were thrown that the a table did not exist. The mechanism for 
deferring the creation of the constraints was there, just not used.

2. on a "sqlobject-admin sql" where the class had a RelatedJoin, the 
intermediary classes did not have a ';' after their CREATE TABLE section, 
thus resulting in invalid SQL. This required more touching as I had the 
relevant functions returns lists of statements that are then joined by ';\n' 
or '\n' depending on how it's being used.

Let me know if you have any questions or need changes made.

-gabe
Index: sqlobject/manager/command.py
===================================================================
--- sqlobject/manager/command.py	(revision 1722)
+++ sqlobject/manager/command.py	(working copy)
@@ -574,14 +574,14 @@
                 if self.options.interactive:
                     if self.ask('Create %s' % soClass.__name__):
                         created += 1
-                        tableConstraints = soClass.createTable()
+                        tableConstraints = soClass.createTable(applyConstraints=False)
                         if tableConstraints:
                             constraints[soClass._connection].append(tableConstraints)
                     else:
                         print 'Cancelled'
                 else:
                     created += 1
-                    tableConstraints = soClass.createTable()
+                    tableConstraints = soClass.createTable(applyConstraints=False)
                     if tableConstraints:
                         constraints[soClass._connection].append(tableConstraints)
         for connection in constraints.keys():
Index: sqlobject/manager/command.py
===================================================================
--- sqlobject/manager/command.py	(revision 1722)
+++ sqlobject/manager/command.py	(working copy)
@@ -506,7 +506,8 @@
             if self.options.verbose >= 1:
                 print '-- %s from %s' % (
                     cls.__name__, cls.__module__)
-            createSql, constraints = cls.createTableSQL()
+            sqlStatements, constraints = cls.createTableSQL()
+            createSql = ';\n'.join(sqlStatements)
             print createSql.strip() + ';\n'
             allConstraints.append(constraints)
         for constraints in allConstraints:
@@ -568,7 +569,7 @@
                     print 'Creating %s' % soClass.__name__
             if v >= 2:
                 sql, extra = soClass.createTableSQL()
-                print sql
+                print ';\n'.join(sql)
             if (not self.options.simulate
                 and not exists):
                 if self.options.interactive:
@@ -860,7 +861,8 @@
                               + '_' + dbName + '.sql')
             if sim:
                 continue
-            create, constraints = cls.createTableSQL()
+            createStatements, constraints = cls.createTableSQL()
+            create = '\n'.join(createStatements)
             if constraints:
                 constraints = '\n-- Constraints:\n%s\n' % (
                     '\n'.join(constraints))
Index: sqlobject/main.py
===================================================================
--- sqlobject/main.py	(revision 1722)
+++ sqlobject/main.py	(working copy)
@@ -1382,11 +1382,12 @@
                        connection=None):
         conn = connection or cls._connection
         sql, constraints = conn.createTableSQL(cls)
+        sqlStatements=[sql]
         if createJoinTables:
-            sql += '\n' + cls.createJoinTablesSQL(connection=conn)
+            sqlStatements.extend(cls.createJoinTablesSQL(connection=conn))
         if createIndexes:
-            sql += '\n' + cls.createIndexesSQL(connection=conn)
-        return sql, constraints
+            sqlStatements.extend(cls.createIndexesSQL(connection=conn))
+        return sqlStatements, constraints
     createTableSQL = classmethod(createTableSQL)
 
     def createJoinTables(cls, ifNotExists=False, connection=None):
@@ -1409,7 +1410,7 @@
                 # This join has requested not to be created
                 continue
             sql.append(conn._SO_createJoinTableSQL(join))
-        return '\n'.join(sql)
+        return sql
     createJoinTablesSQL = classmethod(createJoinTablesSQL)
 
     def createIndexes(cls, ifNotExists=False, connection=None):
@@ -1427,7 +1428,7 @@
             if not index:
                 continue
             sql.append(conn.createIndexSQL(cls, index))
-        return '\n'.join(sql)
+        return sql
     createIndexesSQL = classmethod(createIndexesSQL)
 
     def _getJoinsToCreate(cls):

Reply via email to