google-code-export / django-pyodbc

Automatically exported from code.google.com/p/django-pyodbc
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

compiler.py not appending IDENTITY_INSERT #88

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
While trying to run a syncdb with some initial_data in it, I found that the the 
db keep raising these errors:

  File "C:\Python26\lib\site-packages\django\core\serializers\base.py", line 165, in save
    models.Model.save_base(self.object, using=using, raw=True)
  File "C:\Python26\lib\site-packages\django\db\models\base.py", line 528, in save_base
    result = manager._insert(values, return_id=update_pk, using=using)
  File "C:\Python26\lib\site-packages\django\db\models\manager.py", line 195, in _insert
    return insert_query(self.model, values, **kwargs)
  File "C:\Python26\lib\site-packages\django\db\models\query.py", line 1479, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File "C:\Python26\lib\site-packages\django\db\models\sql\compiler.py", line 783, in execute_sql
    cursor = super(SQLInsertCompiler, self).execute_sql(None)
  File "C:\Python26\lib\site-packages\django\db\models\sql\compiler.py", line 727, in execute_sql
    cursor.execute(sql, params)
  File "C:\Python26\lib\site-packages\django\db\backends\util.py", line 15, in execute
    return self.cursor.execute(sql, params)
  File "C:\Python26\lib\site-packages\sql_server\pyodbc\base.py", line 313, in execute
    return self.cursor.execute(sql, params)
IntegrityError: ('23000', "[23000] [Microsoft][ODBC SQL Server Driver][SQL 
Server]Cannot insert explicit value for identity column in table 
'zinaflow_workflow'
when IDENTITY_INSERT is set to OFF. (544) (SQLExecDirectW)"

I checked the django-mssql code, and I found that this problem was already 
fixed there, this is how they implemented the wrapper:

class SQLInsertCompiler(compiler.SQLInsertCompiler, SQLCompiler):
    def as_sql(self, *args, **kwargs):
        # Fix for Django ticket #14019
        if not hasattr(self, 'return_id'):
            self.return_id = False

        sql, params = super(SQLInsertCompiler, self).as_sql(*args, **kwargs)

        meta = self.query.get_meta()

        if meta.has_auto_field:
            # db_column is None if not explicitly specified by model field
            auto_field_column = meta.auto_field.db_column or meta.auto_field.column

            if auto_field_column in self.query.columns:
                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, params

After doing these changes in compiler.py the IDENTITY was inserted correctly 

Original issue reported on code.google.com by cesar.ca...@gmail.com on 28 Aug 2010 at 9:40

GoogleCodeExporter commented 9 years ago
Patch added.

Original comment by cesar.ca...@gmail.com on 30 Aug 2010 at 8:28

Attachments:

GoogleCodeExporter commented 9 years ago
I cant get this patch to apply, Any change of one that will apply to head?

Original comment by djmar...@gmail.com on 23 Nov 2010 at 10:45

GoogleCodeExporter commented 9 years ago
I've re-created these changed by hand. I haven't tested them.

Original comment by rlaa...@gmail.com on 24 Nov 2010 at 2:08

Attachments:

GoogleCodeExporter commented 9 years ago
That last patch did not apply cleanly. My attached patch (which is nearly 
identical) applies cleanly for me.

Original comment by djfis...@gmail.com on 30 Nov 2010 at 11:58

Attachments:

GoogleCodeExporter commented 9 years ago
Any reason we cant apply this to HEAD?

Original comment by djmar...@gmail.com on 1 Dec 2010 at 2:13

GoogleCodeExporter commented 9 years ago

Original comment by vcc.ch...@gmail.com on 24 Mar 2011 at 4:43

GoogleCodeExporter commented 9 years ago
Fixed in r187, thanks.

Original comment by vcc.ch...@gmail.com on 17 Apr 2011 at 4:51