'Activate IDENTITY_INSERT if needed.'

def pre_exec(self):

    if self.isinsert:
        tbl = self.compiled.statement.table
        seq_column = tbl._autoincrement_column
        insert_has_sequence = (seq_column is not None)
        if insert_has_sequence:
            self._enable_identity_insert = (seq_column.key in self.compiled_parameters[0])
        else:
            self._enable_identity_insert = False
        self._select_lastrowid = (insert_has_sequence and (not self.compiled.returning) and (not self._enable_identity_insert) and (not self.executemany))
        if self._enable_identity_insert:
            self.root_connection._cursor_execute(self.cursor, ('SET    IDENTITY_INSERT    %s    ON' % self.dialect.identifier_preparer.format_table(tbl)), (), self)
