<?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 Re: MetaData Framework for Multi-Level Silver Layer PK/FK Creation in Databricks Free Edition Help</title>
    <link>https://community.databricks.com/t5/databricks-free-edition-help/metadata-framework-for-multi-level-silver-layer-pk-fk-creation/m-p/167701#M895</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/219024"&gt;@SantiNath_Dey&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Databricks has solid building blocks for this, and there's even a purpose-built metadata framework you can lean on.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;At its core, your pipeline needs to do three things...generate surrogate keys at each level, resolve parent references top-down, and declare the constraints. Here's how to approach each of them individually.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;For the first one... surrogate key generation... Databricks supports &lt;A href="https://learn.microsoft.com/en-us/azure/databricks/tables/features/generated-columns" target="_blank"&gt;generated columns&lt;/A&gt; using&amp;nbsp;GENERATED ALWAYS AS IDENTITY&amp;nbsp;or&amp;nbsp;GENERATED BY DEFAULT AS IDENTITY&amp;nbsp;on Delta tables.&amp;nbsp;&amp;nbsp;However, identity columns disable concurrent writes and don't survive a full table refresh (a rebuild can reassign different IDs to the same entity). For your use case involving hierarchical data that may be reprocessed, Databricks recommends&amp;nbsp;using deterministic surrogate keys&amp;nbsp;derived from the natural key instead.&amp;nbsp;&amp;nbsp;For example, you could derive an order-preserving surrogate from a composite natural key rather than using&amp;nbsp;sha2()&amp;nbsp;(which scatters data and hurts clustering performance). Check &lt;A href="https://learn.microsoft.com/en-us/azure/databricks/ldp/best-practices/dimensional-modeling" target="_blank"&gt;this&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;For the second one... top-down parent-child resolution...&amp;nbsp;Since your hierarchy is&amp;nbsp;emp → emp_addr → emp_addr_ofc, you process tables in dependency order. Generate the surrogate key for&amp;nbsp;emp&amp;nbsp;first, then when processing&amp;nbsp;emp_addr, look up the parent's surrogate key via the natural key relationship and store it as the foreign key column. Repeat for&amp;nbsp;emp_addr_ofc&amp;nbsp;referencing&amp;nbsp;emp_addr. A metadata config table can automatically drive this ordering.&lt;/P&gt;
&lt;P&gt;And for the &lt;A href="https://learn.microsoft.com/en-us/azure/databricks/tables/constraints" target="_blank"&gt;constraints&lt;/A&gt;... once your tables are loaded with the correct key columns, you register the relationships using&amp;nbsp;ALTER TABLE... ADD CONSTRAINT.&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;-- Level 1: emp
ALTER TABLE silver.emp ADD CONSTRAINT pk_emp PRIMARY KEY (emp_sk);

-- Level 2: emp_addr
ALTER TABLE silver.emp_addr ADD CONSTRAINT pk_emp_addr PRIMARY KEY (emp_addr_sk);
ALTER TABLE silver.emp_addr ADD CONSTRAINT fk_emp_addr_emp 
  FOREIGN KEY (emp_sk) REFERENCES silver.emp;

-- Level 3: emp_addr_ofc
ALTER TABLE silver.emp_addr_ofc ADD CONSTRAINT pk_emp_addr_ofc PRIMARY KEY (emp_addr_ofc_sk);
ALTER TABLE silver.emp_addr_ofc ADD CONSTRAINT fk_emp_addr_ofc_addr 
  FOREIGN KEY (emp_addr_sk) REFERENCES silver.emp_addr;&lt;/LI-CODE&gt;
&lt;P&gt;These are&amp;nbsp;informational constraints&amp;nbsp;in Unity Catalog. They are not enforced at write time, but they serve as metadata for BI tools, query optimizers, and documentation. If you need actual enforcement, pair them with&amp;nbsp;NOT NULL&amp;nbsp;constraints and CHECK constraints, or validate in your pipeline logic before writing.&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Rather than hand-coding each table's key generation and constraint registration, you can define a metadata configuration (JSON or a Delta table) that describes the hierarchy:&lt;/SPAN&gt;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;[
  {"table": "emp",          "natural_key": ["emp_id"],                    "surrogate_key": "emp_sk",          "parent": null},
  {"table": "emp_addr",     "natural_key": ["emp_id", "addr_id"],        "surrogate_key": "emp_addr_sk",     "parent": {"table": "emp", "join_key": ["emp_id"]}},
  {"table": "emp_addr_ofc", "natural_key": ["emp_id", "addr_id", "ofc_id"], "surrogate_key": "emp_addr_ofc_sk", "parent": {"table": "emp_addr", "join_key": ["emp_id", "addr_id"]}}
]&lt;/LI-CODE&gt;
&lt;P&gt;A generic Python script reads this config, topologically sorts by parent dependencies, and for each table: (1) generates the deterministic surrogate key from the natural key columns, (2) joins to the parent table to resolve the parent's surrogate key as the FK column, and (3) issues the&amp;nbsp;ALTER TABLE ADD CONSTRAINT&amp;nbsp;statements.&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If you want a production-grade metadata-driven framework, take a look at&amp;nbsp;&lt;A href="https://learn.microsoft.com/en-us/azure/databricks/ldp/developer/sdp-meta" target="_blank"&gt;sdp-meta&lt;/A&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;(formerly dlt-meta) from Databricks Labs.&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;It's a metaprogramming framework designed for exactly this pattern... you maintain JSON/YAML metadata describing your tables, and it dynamically generates Lakeflow pipelines for your bronze and silver layers. The benefits are consistency across hundreds of tables, less custom code, and easier maintenance since you're updating config files rather than pipeline logic.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="p1"&gt;&lt;FONT size="2" color="#FF6600"&gt;&lt;STRONG&gt;&lt;I&gt;If this answer resolves your question, could you mark it as “Accept as Solution”? That helps other users quickly find the correct fix.&lt;/I&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 06 Sep 2026 15:29:53 GMT</pubDate>
    <dc:creator>Ashwin_DSA</dc:creator>
    <dc:date>2026-09-06T15:29:53Z</dc:date>
    <item>
      <title>MetaData Framework for Multi-Level Silver Layer PK/FK Creation</title>
      <link>https://community.databricks.com/t5/databricks-free-edition-help/metadata-framework-for-multi-level-silver-layer-pk-fk-creation/m-p/167700#M894</link>
      <description>&lt;P&gt;Hi Team,&lt;BR /&gt;Our source data originates from MongoDB as hierarchical JSON and is ingested into our Silver layer, where it is already normalized into tabular structures. However, primary and foreign key constraints are not enforced during ingestion. We must establish and maintain PK/FK relational integrity post-loading through an automated post-processing step.The Challenge:The source system features a deep, multi-level parent-child tree hierarchy (e.g., emp -&amp;gt; emp_addr -&amp;gt; emp_addr_ofc). We require a metadata-driven modeling framework and script execution strategy to dynamically generate surrogate keys, resolve parent-child references top-down, and enforce relational integrity across all levels.&lt;/P&gt;&lt;P&gt;Please find below screen shot for your reference.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SantiNath_Dey_0-1788703344844.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/30790iFDFDAFEA48C5A707/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SantiNath_Dey_0-1788703344844.png" alt="SantiNath_Dey_0-1788703344844.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 06 Sep 2026 14:02:56 GMT</pubDate>
      <guid>https://community.databricks.com/t5/databricks-free-edition-help/metadata-framework-for-multi-level-silver-layer-pk-fk-creation/m-p/167700#M894</guid>
      <dc:creator>SantiNath_Dey</dc:creator>
      <dc:date>2026-09-06T14:02:56Z</dc:date>
    </item>
    <item>
      <title>Re: MetaData Framework for Multi-Level Silver Layer PK/FK Creation</title>
      <link>https://community.databricks.com/t5/databricks-free-edition-help/metadata-framework-for-multi-level-silver-layer-pk-fk-creation/m-p/167701#M895</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/219024"&gt;@SantiNath_Dey&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Databricks has solid building blocks for this, and there's even a purpose-built metadata framework you can lean on.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;At its core, your pipeline needs to do three things...generate surrogate keys at each level, resolve parent references top-down, and declare the constraints. Here's how to approach each of them individually.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;For the first one... surrogate key generation... Databricks supports &lt;A href="https://learn.microsoft.com/en-us/azure/databricks/tables/features/generated-columns" target="_blank"&gt;generated columns&lt;/A&gt; using&amp;nbsp;GENERATED ALWAYS AS IDENTITY&amp;nbsp;or&amp;nbsp;GENERATED BY DEFAULT AS IDENTITY&amp;nbsp;on Delta tables.&amp;nbsp;&amp;nbsp;However, identity columns disable concurrent writes and don't survive a full table refresh (a rebuild can reassign different IDs to the same entity). For your use case involving hierarchical data that may be reprocessed, Databricks recommends&amp;nbsp;using deterministic surrogate keys&amp;nbsp;derived from the natural key instead.&amp;nbsp;&amp;nbsp;For example, you could derive an order-preserving surrogate from a composite natural key rather than using&amp;nbsp;sha2()&amp;nbsp;(which scatters data and hurts clustering performance). Check &lt;A href="https://learn.microsoft.com/en-us/azure/databricks/ldp/best-practices/dimensional-modeling" target="_blank"&gt;this&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;For the second one... top-down parent-child resolution...&amp;nbsp;Since your hierarchy is&amp;nbsp;emp → emp_addr → emp_addr_ofc, you process tables in dependency order. Generate the surrogate key for&amp;nbsp;emp&amp;nbsp;first, then when processing&amp;nbsp;emp_addr, look up the parent's surrogate key via the natural key relationship and store it as the foreign key column. Repeat for&amp;nbsp;emp_addr_ofc&amp;nbsp;referencing&amp;nbsp;emp_addr. A metadata config table can automatically drive this ordering.&lt;/P&gt;
&lt;P&gt;And for the &lt;A href="https://learn.microsoft.com/en-us/azure/databricks/tables/constraints" target="_blank"&gt;constraints&lt;/A&gt;... once your tables are loaded with the correct key columns, you register the relationships using&amp;nbsp;ALTER TABLE... ADD CONSTRAINT.&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;-- Level 1: emp
ALTER TABLE silver.emp ADD CONSTRAINT pk_emp PRIMARY KEY (emp_sk);

-- Level 2: emp_addr
ALTER TABLE silver.emp_addr ADD CONSTRAINT pk_emp_addr PRIMARY KEY (emp_addr_sk);
ALTER TABLE silver.emp_addr ADD CONSTRAINT fk_emp_addr_emp 
  FOREIGN KEY (emp_sk) REFERENCES silver.emp;

-- Level 3: emp_addr_ofc
ALTER TABLE silver.emp_addr_ofc ADD CONSTRAINT pk_emp_addr_ofc PRIMARY KEY (emp_addr_ofc_sk);
ALTER TABLE silver.emp_addr_ofc ADD CONSTRAINT fk_emp_addr_ofc_addr 
  FOREIGN KEY (emp_addr_sk) REFERENCES silver.emp_addr;&lt;/LI-CODE&gt;
&lt;P&gt;These are&amp;nbsp;informational constraints&amp;nbsp;in Unity Catalog. They are not enforced at write time, but they serve as metadata for BI tools, query optimizers, and documentation. If you need actual enforcement, pair them with&amp;nbsp;NOT NULL&amp;nbsp;constraints and CHECK constraints, or validate in your pipeline logic before writing.&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Rather than hand-coding each table's key generation and constraint registration, you can define a metadata configuration (JSON or a Delta table) that describes the hierarchy:&lt;/SPAN&gt;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;[
  {"table": "emp",          "natural_key": ["emp_id"],                    "surrogate_key": "emp_sk",          "parent": null},
  {"table": "emp_addr",     "natural_key": ["emp_id", "addr_id"],        "surrogate_key": "emp_addr_sk",     "parent": {"table": "emp", "join_key": ["emp_id"]}},
  {"table": "emp_addr_ofc", "natural_key": ["emp_id", "addr_id", "ofc_id"], "surrogate_key": "emp_addr_ofc_sk", "parent": {"table": "emp_addr", "join_key": ["emp_id", "addr_id"]}}
]&lt;/LI-CODE&gt;
&lt;P&gt;A generic Python script reads this config, topologically sorts by parent dependencies, and for each table: (1) generates the deterministic surrogate key from the natural key columns, (2) joins to the parent table to resolve the parent's surrogate key as the FK column, and (3) issues the&amp;nbsp;ALTER TABLE ADD CONSTRAINT&amp;nbsp;statements.&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If you want a production-grade metadata-driven framework, take a look at&amp;nbsp;&lt;A href="https://learn.microsoft.com/en-us/azure/databricks/ldp/developer/sdp-meta" target="_blank"&gt;sdp-meta&lt;/A&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;(formerly dlt-meta) from Databricks Labs.&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;It's a metaprogramming framework designed for exactly this pattern... you maintain JSON/YAML metadata describing your tables, and it dynamically generates Lakeflow pipelines for your bronze and silver layers. The benefits are consistency across hundreds of tables, less custom code, and easier maintenance since you're updating config files rather than pipeline logic.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="p1"&gt;&lt;FONT size="2" color="#FF6600"&gt;&lt;STRONG&gt;&lt;I&gt;If this answer resolves your question, could you mark it as “Accept as Solution”? That helps other users quickly find the correct fix.&lt;/I&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 06 Sep 2026 15:29:53 GMT</pubDate>
      <guid>https://community.databricks.com/t5/databricks-free-edition-help/metadata-framework-for-multi-level-silver-layer-pk-fk-creation/m-p/167701#M895</guid>
      <dc:creator>Ashwin_DSA</dc:creator>
      <dc:date>2026-09-06T15:29:53Z</dc:date>
    </item>
    <item>
      <title>Re: MetaData Framework for Multi-Level Silver Layer PK/FK Creation</title>
      <link>https://community.databricks.com/t5/databricks-free-edition-help/metadata-framework-for-multi-level-silver-layer-pk-fk-creation/m-p/167703#M896</link>
      <description>&lt;P&gt;Thank you for quick response&lt;/P&gt;</description>
      <pubDate>Sun, 06 Sep 2026 16:11:17 GMT</pubDate>
      <guid>https://community.databricks.com/t5/databricks-free-edition-help/metadata-framework-for-multi-level-silver-layer-pk-fk-creation/m-p/167703#M896</guid>
      <dc:creator>SantiNath_Dey</dc:creator>
      <dc:date>2026-09-06T16:11:17Z</dc:date>
    </item>
  </channel>
</rss>

