title: A schema mismatch detected when writing to the Delta table - Azure Databricks
body: <p>I try to load &quot;small_radio_json.json&quot; to Delta Lake table. After this code I would create table.</p>
  <p>I try create Delta table but getting error &quot;A schema mismatch detected when writing to the Delta table.&quot;
  It may be related to partition of the  <code>events.write.format(&quot;delta&quot;).mode(&quot;overwrite&quot;).partitionBy(&quot;artist&quot;).save(&quot;/delta/events/&quot;)</code></p>
  <p>How to fix or modify code.</p>
  <pre class="lang-scala prettyprint-override"><code>    //https://docs.microsoft.com/en-us/azure/azure-databricks/databricks-extract-load-sql-data-warehouse
      //https://docs.microsoft.com/en-us/azure/databricks/_static/notebooks/delta/quickstart-scala.html
      
      //Session configuration
      val appID = &quot;123558b9-3525-4c62-8c48-d3d7e2c16a6a&quot;
      val secret = &quot;123[xEPjpOIBJtBS-W9B9Zsv7h9IF:qw&quot;
      val tenantID = &quot;12344839-0afa-4fae-a34a-326c42112bca&quot;
  
      spark.conf.set(&quot;fs.azure.account.auth.type&quot;, &quot;OAuth&quot;)
      spark.conf.set(&quot;fs.azure.account.oauth.provider.type&quot;, 
      &quot;org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider&quot;)
      spark.conf.set(&quot;fs.azure.account.oauth2.client.id&quot;, &quot;&lt;appID&gt;&quot;)
      spark.conf.set(&quot;fs.azure.account.oauth2.client.secret&quot;, &quot;&lt;secret&gt;&quot;)
     spark.conf.set(&quot;fs.azure.account.oauth2.client.endpoint&quot;, &quot;https://login.microsoftonline.com/&lt;tenant- 
     id&gt;/oauth2/token&quot;)
     spark.conf.set(&quot;fs.azure.createRemoteFileSystemDuringInitialization&quot;, &quot;true&quot;)
  
     //Account Information
      val storageAccountName = &quot;mydatalake&quot;
     val fileSystemName = &quot;fileshare1&quot;
  
      spark.conf.set(&quot;fs.azure.account.auth.type.&quot; + storageAccountName + &quot;.dfs.core.windows.net&quot;, &quot;OAuth&quot;)
      spark.conf.set(&quot;fs.azure.account.oauth.provider.type.&quot; + storageAccountName + 
      &quot;.dfs.core.windows.net&quot;, &quot;org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider&quot;)
      spark.conf.set(&quot;fs.azure.account.oauth2.client.id.&quot; + storageAccountName + &quot;.dfs.core.windows.net&quot;, 
      &quot;&quot; + appID + &quot;&quot;)
      spark.conf.set(&quot;fs.azure.account.oauth2.client.secret.&quot; + storageAccountName + 
      &quot;.dfs.core.windows.net&quot;, &quot;&quot; + secret + &quot;&quot;)
      spark.conf.set(&quot;fs.azure.account.oauth2.client.endpoint.&quot; + storageAccountName + 
      &quot;.dfs.core.windows.net&quot;, &quot;https://login.microsoftonline.com/&quot; + tenantID + &quot;/oauth2/token&quot;)
      spark.conf.set(&quot;fs.azure.createRemoteFileSystemDuringInitialization&quot;, &quot;true&quot;)
      dbutils.fs.ls(&quot;abfss://&quot; + fileSystemName  + &quot;@&quot; + storageAccountName + &quot;.dfs.core.windows.net/&quot;)
      spark.conf.set(&quot;fs.azure.createRemoteFileSystemDuringInitialization&quot;, &quot;false&quot;)
  
      dbutils.fs.cp(&quot;file:///tmp/small_radio_json.json&quot;, &quot;abfss://&quot; + fileSystemName + &quot;@&quot; + 
      storageAccountName + &quot;.dfs.core.windows.net/&quot;)
  
      val df = spark.read.json(&quot;abfss://&quot; + fileSystemName + &quot;@&quot; + storageAccountName + 
     &quot;.dfs.core.windows.net/small_radio_json.json&quot;)
  
      //df.show()
  
      import org.apache.spark.sql._
     import org.apache.spark.sql.functions._
  
      val events = df
    
      display(events)
  
      import org.apache.spark.sql.SaveMode
  
      events.write.format(&quot;delta&quot;).mode(&quot;overwrite&quot;).partitionBy(&quot;artist&quot;).save(&quot;/delta/events/&quot;)
      import org.apache.spark.sql.SaveMode
  
     val events_delta = spark.read.format(&quot;delta&quot;).load(&quot;/delta/events/&quot;)
      display(events_delta)
  </code></pre>
  <p>The exception:</p>
  <pre><code>    org.apache.spark.sql.AnalysisException: A schema mismatch detected when writing to the Delta table.
      To enable schema migration, please set:
      '.option(&quot;mergeSchema&quot;, &quot;true&quot;)'.
  
      Table schema:
      root
      -- action: string (nullable = true)
      -- date: string (nullable = true)
  
  
      Data schema:
      root
      -- artist: string (nullable = true)
      -- auth: string (nullable = true)
      -- firstName: string (nullable = true)
      -- gender: string (nullable = true)
  </code></pre>