title: Kafka Connect + JDBC Source connector + JDBC Sink connector + MSSQL SQL Server = IDENTITY_INSERT issues
body: <p>I am trying to figure out why I am receiving an &quot;IDENTITY_INSERT&quot; error when trying to use a JDBC sink connector to sink data into a SQL Server database off of a topic that is also written to by a JDBC source connector connected to the same SQL Server database.</p>
  <p><strong>The overall objective</strong>:</p>
  <p>Currently there is a SQL Server database being used by the backend for storage in a traditional sense, and we are trying to transition to using Kafka for all of the same purposes, however the SQL Server database must remain for the time being as there are services that still rely upon it, and we have a requirement that all data that is on Kafka be mirrored in the SQL Server database.</p>
  <p><strong>What I am trying to accomplish</strong>:</p>
  <p>I am trying to create a setup in which there are the following:</p>
  <ul>
  <li>One SQL Server database (<strong>all tables with identical primary key of &quot;id&quot; which auto-increments and is set by SQL Server</strong>)</li>
  <li>Kafka cluster, including Kafka connect with:
  <ul>
  <li>JDBC Source connector to sync what is in the SQL Server table onto a kafka topic, lets call it <em>AccountType</em> for both the topic and the table</li>
  <li>JD Sink connector that subscribes to the same topic <em>AccountType</em> and sinks data into the same <em>AccountType</em> table in the SQL Server Database</li>
  </ul>
  </li>
  </ul>
  <p><strong>The expected behavior is</strong>:</p>
  <ul>
  <li>if a legacy service writes/updates a record in SQL Server
  <ul>
  <li>the source connector will pick up the change and write it to it's corresponding Kafka topic</li>
  <li>The sink connector will receive the message on that same topic, however, since the change originated in SQL Server and therefore has already been made from the perspective of the sink connector the sink connector will find a match on the primary key, see that there is no change to be made, and move along</li>
  </ul>
  </li>
  <li>if a new service, designed to work with Kafka, updates a record and writes it to the correct topic:
  <ul>
  <li>the JDBC sink connector will receive the message on the topic as an offset</li>
  <li>since the sink connector was configured with upsert mode, it will find a match on the primary key in the target database and update the corresponding record in the target database</li>
  <li>the source connector will then detect the change, triggering it to write the change to the corresponding topic</li>
  <li>My assumption at this point is that one of two things will happen, either:
  <ul>
  <li>the source connector will not write to the topic since it would only be duplicating the last message or</li>
  <li>the source connector will write the duplicate message to the topic, however it will be ignored by the sink as there would be no resulting database record changes</li>
  </ul>
  </li>
  </ul>
  </li>
  </ul>
  <p>This expected behavior aligns with everything I have found in the documentation, and is as best as I can tell in accordance with the JDBC sink deep dive guide found here: <a href="https://rmoff.net/2021/03/12/kafka-connect-jdbc-sink-deep-dive-working-with-primary-keys/%5Bkafka-connect-jdbc-sink-deep-dive-working-with-primary-keys%5D%5B1%5D" rel="nofollow noreferrer">https://rmoff.net/2021/03/12/kafka-connect-jdbc-sink-deep-dive-working-with-primary-keys/[kafka-connect-jdbc-sink-deep-dive-working-with-primary-keys][1]</a></p>
  <p><strong>What is happening instead</strong>:</p>
  <ul>
  <li>with the Kafka cluster all started up, and the database empty, both connectors are successfully created</li>
  <li>A row is inserted into a table in the database using an external service</li>
  <li>the source connector successfully picks up the change and writes a record to the topic on Kafka (which has been split by the transforms such that the field representing the SQL Server table PK has been extracted and set as the message key, and removed from the value)</li>
  <li>(the problem) the sink connector then receives the message on that topic and...</li>
  </ul>
  <p>...here's the issue, based on several videos and examples I could find, nothing should happen since that record is already up to date in the database, instead, however, it immediately attempts to write the entire message as-is, to the target table resulting in the following:</p>
  <blockquote>
  <p>java.sql.BatchUpdateException: Cannot insert explicit value for identity column in table 'AccountType' when IDENTITY_INSERT is set to OFF.</p>
  </blockquote>
  <p>Which makes sense, since the message from the topic has a primary key field in it, if it isn't enabled in the table then it shouldn't be allowed. Just for fun, I tried throwing in an additional transform to remove the id field before trying to write, and instead using another field in the table that has a &quot;Unique&quot; constraint in the configuration. When I repeated the steps this time it did not complain about writing the primary key, however it did still immediately try and insert the record which resulted in another error since it would violate the unique constraint, which again makes perfect sense.</p>
  <p><strong>Where I am stuck</strong>:</p>
  <p>If all of the above makes sense can anyone tell me why it is automatically trying to insert despite being set to upsert?</p>
  <p><strong>Notes</strong>:</p>
  <ul>
  <li>all of this is being set up using docker containers provided by confluent for the confluent platform version 6.2.0</li>
  </ul>
  <p><strong>Source connector configuration</strong>:</p>
  <pre><code>{
    &quot;connection.url&quot;: &quot;jdbc:sqlserver://mssql:1433;databaseName=REDACTED&quot;,
    &quot;connection.user&quot;:&quot;REDACTED&quot;,
    &quot;connection.password&quot;:&quot;REDACTED&quot;,
    &quot;connection.attempts&quot;: &quot;3&quot;,
    &quot;connection.backoff.ms&quot;: &quot;5000&quot;,
    &quot;table.whitelist&quot;: &quot;AccountType&quot;,
    &quot;db.timezone&quot;: &quot;UTC&quot;,
    &quot;name&quot;: &quot;sql-server-source&quot;,
    &quot;connector.class&quot;: &quot;io.confluent.connect.jdbc.JdbcSourceConnector&quot;,
    &quot;dialect.name&quot;: &quot;SqlServerDatabaseDialect&quot;,
    &quot;config.action.reload&quot;: &quot;restart&quot;,
    &quot;topic.creation.enable&quot;: &quot;false&quot;,
    &quot;tasks.max&quot;: &quot;1&quot;,
    &quot;mode&quot;: &quot;timestamp+incrementing&quot;,
    &quot;incrementing.column.name&quot;: &quot;id&quot;,
    &quot;timestamp.column.name&quot;: &quot;created, updated&quot;,
    &quot;validate.non.null&quot;: true,
    &quot;key.converter&quot;: &quot;org.apache.kafka.connect.converters.LongConverter&quot;,
    &quot;value.converter&quot;: &quot;io.confluent.connect.json.JsonSchemaConverter&quot;,
    &quot;value.converter.schema.registry.url&quot;: &quot;http://schema-registry:8081&quot;,
    &quot;auto.register.schemas&quot;: &quot;true&quot;,
    &quot;schema.registry.url&quot;: &quot;http://schema-registry:8081&quot;,
    &quot;errors.log.include.messages&quot;: &quot;true&quot;,
    &quot;transforms&quot;: &quot;copyFieldToKey,extractKeyFromStruct,removeKeyFromValue&quot;,
    &quot;transforms.copyFieldToKey.type&quot;: &quot;org.apache.kafka.connect.transforms.ValueToKey&quot;,
    &quot;transforms.copyFieldToKey.fields&quot;: &quot;id&quot;,
    &quot;transforms.extractKeyFromStruct.type&quot;:                         
    &quot;org.apache.kafka.connect.transforms.ExtractField$Key&quot;,
    &quot;transforms.extractKeyFromStruct.field&quot;: &quot;id&quot;,
    &quot;transforms.removeKeyFromValue.type&quot;: 
    &quot;org.apache.kafka.connect.transforms.ReplaceField$Value&quot;,
    &quot;transforms.removeKeyFromValue.blacklist&quot;: &quot;id&quot;,
    &quot;transforms&quot;: &quot;copyFieldToKey,extractKeyFromStruct,removeKeyFromValue&quot;,
    &quot;transforms.copyFieldToKey.type&quot;: &quot;org.apache.kafka.connect.transforms.ValueToKey&quot;,
    &quot;transforms.copyFieldToKey.fields&quot;: &quot;id&quot;,
    &quot;transforms.extractKeyFromStruct.type&quot;: 
    &quot;org.apache.kafka.connect.transforms.ExtractField$Key&quot;,
    &quot;transforms.extractKeyFromStruct.field&quot;: &quot;id&quot;,
    &quot;transforms.removeKeyFromValue.type&quot;: 
    &quot;org.apache.kafka.connect.transforms.ReplaceField$Value&quot;,
    &quot;transforms.removeKeyFromValue.blacklist&quot;: &quot;id&quot;,
  }
  </code></pre>
  <p><strong>Sink connector configuration</strong>:</p>
  <pre><code>{
    &quot;connection.url&quot;: &quot;jdbc:sqlserver://mssql:1433;databaseName=REDACTED&quot;,
    &quot;connection.user&quot;:&quot;REDACTED&quot;,
    &quot;connection.password&quot;:&quot;REDACTED&quot;,
    &quot;connection.attempts&quot;: &quot;3&quot;,
    &quot;connection.backoff.ms&quot;: &quot;5000&quot;,
    &quot;table.name.format&quot;: &quot;${topic}&quot;,
    &quot;db.timezone&quot;: &quot;UTC&quot;,
    &quot;name&quot;: &quot;sql-server-sink&quot;,
    &quot;connector.class&quot;: &quot;io.confluent.connect.jdbc.JdbcSinkConnector&quot;,
    &quot;dialect.name&quot;: &quot;SqlServerDatabaseDialect&quot;,
    &quot;auto.create&quot;: &quot;false&quot;,
    &quot;auto.evolve&quot;: &quot;false&quot;,
    &quot;tasks.max&quot;: &quot;1&quot;,
    &quot;batch.size&quot;: &quot;1000&quot;,
    &quot;topics&quot;: &quot;AccountType&quot;,
    &quot;key.converter&quot;: &quot;org.apache.kafka.connect.converters.LongConverter&quot;,
    &quot;value.converter&quot;: &quot;io.confluent.connect.json.JsonSchemaConverter&quot;,
    &quot;value.converter.schema.registry.url&quot;: &quot;http://schema-registry:8081&quot;,
    &quot;insert.mode&quot;: &quot;UPSERT&quot;,
    &quot;pk.mode&quot;: &quot;record_key&quot;,
    &quot;pk.fields&quot;: &quot;id&quot;,
  }
  </code></pre>