Commits

Michael Manfre committed a0cc6cb

Convert insert SQL to handle special case where inserting DEFAULT to primary key. Fixes #5

Comments (0)

Files changed (1)

sqlserver_ado/compiler.py

         result = super(SQLInsertCompiler, self).as_sql(*args, **kwargs)
         if isinstance(result, list):
             # Django 1.4 wraps return in list
-            return [(self._identity_insert(x[0]), x[1]) \
-                for x in result]
+            return [self._fix_insert(x[0], x[1]) for x in result]
         
         sql, params = result
-        return self._identity_insert(sql), params
+        return self._fix_insert(sql, params)
 
-    def _identity_insert(self, sql):
+    def _fix_insert(self, sql, params):
         """
-        Wrap the passed SQL with IDENTITY_INSERT statements.
+        Wrap the passed SQL with IDENTITY_INSERT statements and apply
+        other necessary fixes.
         """
         meta = self.query.get_meta()
         
-        if not meta.has_auto_field:
-            return sql
+        if meta.has_auto_field:
+            if hasattr(self.query, 'fields'):
+                # django 1.4 replaced columns with fields
+                fields = self.query.fields
+                auto_field = meta.auto_field
+            else:
+                # < django 1.4
+                fields = self.query.columns
+                auto_field = meta.auto_field.db_column or meta.auto_field.column
+    
+            auto_in_fields = auto_field in fields
+    
+            quoted_table = self.connection.ops.quote_name(meta.db_table)
+            if not fields or (auto_in_fields and len(fields) == 1 and not params):
+                # convert format when inserting only the primary key without 
+                # specifying a value
+                sql = 'INSERT INTO {0} DEFAULT VALUES'.format(
+                    quoted_table
+                )
+                params = []
+            elif auto_in_fields:
+                # wrap with identity insert
+                sql = "SET IDENTITY_INSERT %s ON;%s;SET IDENTITY_INSERT %s OFF" %\
+                    (quoted_table, sql, quoted_table)
 
-        if hasattr(self.query, 'fields'):
-            # django 1.4 added fields
-            inserting_auto_field = meta.auto_field in self.query.fields
-        else:
-            column = meta.auto_field.db_column or meta.auto_field.column
-            inserting_auto_field = column in self.query.columns
-
-        if inserting_auto_field:
-            quoted_table = self.connection.ops.quote_name(meta.db_table)
-            sql = "SET IDENTITY_INSERT %s ON;%s;SET IDENTITY_INSERT %s OFF" %\
-                (quoted_table, sql, quoted_table)
-
-        return sql
+        return sql, params
 
 class SQLDeleteCompiler(compiler.SQLDeleteCompiler, SQLCompiler):
     pass
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.