<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Unable to add primary key constraint to nullable identity column in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/unable-to-add-primary-key-constraint-to-nullable-identity-column/m-p/121462#M46459</link>
    <description>&lt;P&gt;While we can in fact define a primary key during table creation for an identity column that's nullable (i.e., not constrained using NOT NULL), it's not possible to add such a primary key constraint after the table has been created.&lt;BR /&gt;&lt;BR /&gt;We get an error message:&lt;BR /&gt;&amp;gt; Cannot create the primary key `my_table_pk` because its child column(s) `id` is nullable. Please change the column nullability and retry.&lt;BR /&gt;&lt;BR /&gt;But it's not possible to change the nullability of an identity column:&lt;BR /&gt;&amp;gt; ALTER TABLE ALTER COLUMN is not supported for IDENTITY columns.&lt;/P&gt;&lt;P&gt;I'm not sure if logically speaking this is a bug, but it's a bit of an unfortunate situation if you want to move to a table setup with constraints and haven't created the identity columns initially with a nullability constraint.&lt;/P&gt;</description>
    <pubDate>Wed, 11 Jun 2025 12:39:27 GMT</pubDate>
    <dc:creator>Malthe</dc:creator>
    <dc:date>2025-06-11T12:39:27Z</dc:date>
    <item>
      <title>Unable to add primary key constraint to nullable identity column</title>
      <link>https://community.databricks.com/t5/data-engineering/unable-to-add-primary-key-constraint-to-nullable-identity-column/m-p/121462#M46459</link>
      <description>&lt;P&gt;While we can in fact define a primary key during table creation for an identity column that's nullable (i.e., not constrained using NOT NULL), it's not possible to add such a primary key constraint after the table has been created.&lt;BR /&gt;&lt;BR /&gt;We get an error message:&lt;BR /&gt;&amp;gt; Cannot create the primary key `my_table_pk` because its child column(s) `id` is nullable. Please change the column nullability and retry.&lt;BR /&gt;&lt;BR /&gt;But it's not possible to change the nullability of an identity column:&lt;BR /&gt;&amp;gt; ALTER TABLE ALTER COLUMN is not supported for IDENTITY columns.&lt;/P&gt;&lt;P&gt;I'm not sure if logically speaking this is a bug, but it's a bit of an unfortunate situation if you want to move to a table setup with constraints and haven't created the identity columns initially with a nullability constraint.&lt;/P&gt;</description>
      <pubDate>Wed, 11 Jun 2025 12:39:27 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/unable-to-add-primary-key-constraint-to-nullable-identity-column/m-p/121462#M46459</guid>
      <dc:creator>Malthe</dc:creator>
      <dc:date>2025-06-11T12:39:27Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to add primary key constraint to nullable identity column</title>
      <link>https://community.databricks.com/t5/data-engineering/unable-to-add-primary-key-constraint-to-nullable-identity-column/m-p/121479#M46464</link>
      <description>&lt;P&gt;Creating a table with a nullable IDENTITY column and defining the primary key at creation time works.&lt;BR /&gt;The database quietly interprets the column as NOT NULL for the purposes of the primary key, even though it's technically defined as nullable &lt;SPAN&gt;(i.e., not constrained using NOT NULL).&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The database engine enforces the nullability at the schema level due to which this contradictory error shows up.&amp;nbsp;&lt;BR /&gt;As of today there is no direct fix, maybe we can add this as enhancement but not sure its going to fix anytime soon as this will get tricky and dirty.&amp;nbsp;&lt;BR /&gt;The best approach is dropping the table and recreating it with required constraints during creation ( if possible ).&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Jun 2025 15:19:12 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/unable-to-add-primary-key-constraint-to-nullable-identity-column/m-p/121479#M46464</guid>
      <dc:creator>amuchoudhary</dc:creator>
      <dc:date>2025-06-11T15:19:12Z</dc:date>
    </item>
  </channel>
</rss>

