<?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 Error Creating Primary Key Constraint in DLT in Get Started Discussions</title>
    <link>https://community.databricks.com/t5/get-started-discussions/error-creating-primary-key-constraint-in-dlt/m-p/79981#M7887</link>
    <description>&lt;P&gt;Hello There!&lt;/P&gt;&lt;P&gt;Greetings!!&lt;BR /&gt;&lt;BR /&gt;I am getting the following error when trying to Create a DLT table in my Gold Layer..&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;com.databricks.sql.managedcatalog.PrimaryKeyColumnsNullableException: Cannot create the primary key `x_key` because its child column(s) `x_key` is nullable. Please change the column nullability and retry.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;I have configured the table as below with constraints&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;CREATE&lt;/SPAN&gt; &lt;SPAN&gt;OR&lt;/SPAN&gt; &lt;SPAN&gt;REFRESH&lt;/SPAN&gt;&lt;SPAN&gt; LIVE &lt;/SPAN&gt;&lt;SPAN&gt;TABLE&lt;/SPAN&gt;&lt;SPAN&gt; XYZ&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;CONSTRAINT&lt;/SPAN&gt;&lt;SPAN&gt; pk_key_not_null EXPECT (x_key &lt;/SPAN&gt;&lt;SPAN&gt;IS NOT NULL&lt;/SPAN&gt;&lt;SPAN&gt;) ,&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;CONSTRAINT x_key PRIMARY KEY(x_key)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;.....rest of the code&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;The same code runs fine in my UAT environment but fails in Dev...I am clueless why its happening...can someone help with it?&lt;/DIV&gt;&lt;/DIV&gt;</description>
    <pubDate>Mon, 22 Jul 2024 18:41:58 GMT</pubDate>
    <dc:creator>ani2409</dc:creator>
    <dc:date>2024-07-22T18:41:58Z</dc:date>
    <item>
      <title>Error Creating Primary Key Constraint in DLT</title>
      <link>https://community.databricks.com/t5/get-started-discussions/error-creating-primary-key-constraint-in-dlt/m-p/79981#M7887</link>
      <description>&lt;P&gt;Hello There!&lt;/P&gt;&lt;P&gt;Greetings!!&lt;BR /&gt;&lt;BR /&gt;I am getting the following error when trying to Create a DLT table in my Gold Layer..&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;com.databricks.sql.managedcatalog.PrimaryKeyColumnsNullableException: Cannot create the primary key `x_key` because its child column(s) `x_key` is nullable. Please change the column nullability and retry.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;I have configured the table as below with constraints&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;CREATE&lt;/SPAN&gt; &lt;SPAN&gt;OR&lt;/SPAN&gt; &lt;SPAN&gt;REFRESH&lt;/SPAN&gt;&lt;SPAN&gt; LIVE &lt;/SPAN&gt;&lt;SPAN&gt;TABLE&lt;/SPAN&gt;&lt;SPAN&gt; XYZ&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;CONSTRAINT&lt;/SPAN&gt;&lt;SPAN&gt; pk_key_not_null EXPECT (x_key &lt;/SPAN&gt;&lt;SPAN&gt;IS NOT NULL&lt;/SPAN&gt;&lt;SPAN&gt;) ,&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;CONSTRAINT x_key PRIMARY KEY(x_key)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;.....rest of the code&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;The same code runs fine in my UAT environment but fails in Dev...I am clueless why its happening...can someone help with it?&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 22 Jul 2024 18:41:58 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/error-creating-primary-key-constraint-in-dlt/m-p/79981#M7887</guid>
      <dc:creator>ani2409</dc:creator>
      <dc:date>2024-07-22T18:41:58Z</dc:date>
    </item>
    <item>
      <title>Re: Error Creating Primary Key Constraint in DLT</title>
      <link>https://community.databricks.com/t5/get-started-discussions/error-creating-primary-key-constraint-in-dlt/m-p/79984#M7888</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/113239"&gt;@ani2409&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;So you need to do as they stated in error message. Column that is chosen to be primary key needs to be defined as NOT NULL.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Additionaly, could you check if on DEV environement you don't have null values in x_key column? Because, in this case, the null values wiill violate NOT NULL constraint.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;And it can explain why on UAT the same code works. On UAT you have either:&lt;/P&gt;&lt;P&gt;- differently defined&amp;nbsp;&lt;SPAN&gt;x_key column (with NOT NULL)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;- different data compared to DEV environment (they don't contain NULL values in key column)&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2024 18:51:34 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/error-creating-primary-key-constraint-in-dlt/m-p/79984#M7888</guid>
      <dc:creator>szymon_dybczak</dc:creator>
      <dc:date>2024-07-22T18:51:34Z</dc:date>
    </item>
    <item>
      <title>Re: Error Creating Primary Key Constraint in DLT</title>
      <link>https://community.databricks.com/t5/get-started-discussions/error-creating-primary-key-constraint-in-dlt/m-p/80105#M7889</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/110502"&gt;@szymon_dybczak&lt;/a&gt;&amp;nbsp;for the response.&lt;/P&gt;&lt;P&gt;As you can see I have already defined the not null constraint in my definition for the primary key&amp;nbsp;&lt;SPAN&gt;x_key&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;CONSTRAINT&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;pk_key_not_null EXPECT (x_key&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;IS NOT NULL&lt;/SPAN&gt;&lt;SPAN&gt;)&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;But still I am getting the same error.Also I checked in source to check if we have any null values but we dont...and UAT and DEV have same data set.. &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Jul 2024 09:53:14 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/error-creating-primary-key-constraint-in-dlt/m-p/80105#M7889</guid>
      <dc:creator>ani2409</dc:creator>
      <dc:date>2024-07-23T09:53:14Z</dc:date>
    </item>
    <item>
      <title>Re: Error Creating Primary Key Constraint in DLT</title>
      <link>https://community.databricks.com/t5/get-started-discussions/error-creating-primary-key-constraint-in-dlt/m-p/80117#M7890</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/113239"&gt;@ani2409&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;Maybe try to define column with primary key constraint in a way they do in the documentation:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;CREATE OR REFRESH MATERIALIZED VIEW sales
(customer_id STRING NOT NULL PRIMARY KEY,
  customer_name STRING,
  number_of_line_items STRING,
  order_datetime STRING,
  order_number LONG,
  order_day_of_week STRING GENERATED ALWAYS AS (dayofweek(order_datetime)),
  CONSTRAINT fk_customer_id FOREIGN KEY (customer_id) REFERENCES main.default.customers(customer_id)
)
COMMENT "Raw data on sales"
AS SELECT * FROM ...&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;&lt;A href="https://docs.databricks.com/en/delta-live-tables/sql-ref.html#example-define-table-constraints" target="_blank"&gt;Delta Live Tables SQL language reference | Databricks on AWS&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Jul 2024 11:21:28 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/error-creating-primary-key-constraint-in-dlt/m-p/80117#M7890</guid>
      <dc:creator>szymon_dybczak</dc:creator>
      <dc:date>2024-07-23T11:21:28Z</dc:date>
    </item>
  </channel>
</rss>

