Skip to content

Commit c2bc6de

Browse files
committed
Fix bug when inserting with a specific primary key.
1 parent b1ecdc4 commit c2bc6de

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

sql_server/pyodbc/compiler.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ def as_sql(self):
357357
# db_column is None if not explicitly specified by model field
358358
auto_field_column = meta.auto_field.db_column or meta.auto_field.column
359359

360+
out = []
360361
for item in items:
361362
sql, params = item
362363
if auto_field_column in columns:
@@ -367,7 +368,8 @@ def as_sql(self):
367368
else:
368369
sql = "SET IDENTITY_INSERT %s ON;\n%s;\nSET IDENTITY_INSERT %s OFF" % \
369370
(quoted_table, sql, quoted_table)
370-
item = [sql, params]
371+
out.append([sql, params])
372+
items = out
371373

372374
return items
373375

sql_server/pyodbc/query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def _insert_as_sql(self, *args, **kwargs):
7373
quoted_table = self.connection.ops.quote_name(meta.db_table)
7474
# Get (sql, params) from original InsertQuery.as_sql
7575
sql, params = self._orig_as_sql(*args, **kwargs)
76-
if meta.pk.db_column in self.columns and meta.pk.__class__.__name__ == "AutoField":
76+
if meta.has_auto_field and meta.auto_field.column in self.columns:
7777
if len(self.columns) == 1 and not params:
7878
sql = "INSERT INTO %s DEFAULT VALUES" % quoted_table
7979
else:

0 commit comments

Comments
 (0)