You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support for the RELY property of primary and foreign key constraint #1271
Add native support for the RELY property of primary and foreign key constraint, by adding an optional, boolean input to the constraint types.
Optional feature extension - QoL safeguard
Setting rely: true would implicitly create the not_null and unique constraints to ensure that the constraint is valid/satisfied. This is purely a safeguard against incorrect query results when the rely option is set but not satisfied.
Describe alternatives you've considered
The behavior is already achievable for primary constraints, as they can be implemented directly as expression using the custom constraint type (introduced in #792). However, for foreign key constraints this is not a viable option as the ref macro is not a part of the limited Jinja context for .yaml files.
Who will this benefit?
One of the main reasons for adding PK/FK constraints is for the potential performance enhancement, which requires the RELY option to be set. Hence, most (if not all) users of these constraints on this adapter would benefit from this feature.
Furthermore, the RELY constraint property is also used by snowflake, extending the appeal and useability of the feature.
@dataclassclassTypedConstraint(ModelLevelConstraint, ABC):
"""Constraint that enforces type because it has render logic"""str_type: ClassVar[str]
rely: bool|None=None
Or alternatively, the functionality can be achieved by simply refactoring ForeignKeyConstraint such that expression is used for constraints options similar to the current implementation of PrimaryKeyConstraint:
@Phil-Garmann : thanks for figuring that one out. I got it working
Could you post the schema.yml definition of your primary_key constraint definition? I can't get it working. I've tried "expression: rely" & "expression: "rely". Thank you so much!
@Phil-Garmann : thanks for figuring that one out. I got it working
Could you post the schema.yml definition of your primary_key constraint definition? I can't get it working. I've tried "expression: rely" & "expression: "rely". Thank you so much!
One data point on the "QoL safeguard" in the original proposal, which may matter for the PR in progress: on Databricks, dbt's built-in unique test does not detect duplicates once the primary key is declared RELY. A safeguard built on that test would pass on the data it is meant to catch.
Two table models select the same six rows from a source where order_id = 3 appears twice. Their contracts differ only in the PK option, set with the expression: RELY workaround from earlier in this thread:
3 of 4 FAIL 1 unique_orders_norely_order_id .................................... [FAIL 1 in 1.70s]
4 of 4 PASS unique_orders_rely_order_id ........................................ [PASS in 1.54s]
Why: the test compiles to group by order_id having count(*) > 1. With RELY, the optimizer treats order_id as unique, so every group has one row, and it plans the query as LocalTableScan <empty>. Query history shows 0 rows and 0 files read. On a 1,000,010-row table (16 files, 10 duplicated keys), the NORELY run found all 10 duplicate keys and the RELY run found none. The docs say RELY rewrites need Photon; I only tested on Photon.
A window-based test still catches it. With this override in the project, the RELY model's test fails with 2 rows:
{% macro databricks__test_unique(model, column_name) %}
select unique_field, n_records
from (
select
{{ column_name }} as unique_field,
count(*) over (partition by {{ column_name }}) as n_records
from {{ model }}
where {{ column_name }} is not null
) counted
where n_records >1
{% endmacro %}
What this means for the safeguard
A unique data test that runs after the constraint exists, which is the normal dbt build order, passes on violating data.
A unique constraint doesn't help either, because Databricks does not enforce UNIQUE constraints.
Two approaches that would work: validate uniqueness before the constraint is declared with RELY, or have the adapter's unique test count with a window when RELY is set. The window version held in my runs, but I found no documentation promising the optimizer will leave it alone.
Activity
MCRE-BE commentedon Dec 26, 2025
I was exactly trying to use this feature and failing. Good to see why.
Hope it gets implemented !
Phil-Garmann commentedon Jan 5, 2026
Mockup
constraints.pytest_constraints.pytest_constraint_macros.pyPhil-Garmann commentedon Jan 6, 2026
Or alternatively, the functionality can be achieved by simply refactoring
ForeignKeyConstraintsuch thatexpressionis used for constraints options similar to the current implementation ofPrimaryKeyConstraint:MCRE-BE commentedon Mar 18, 2026
I like the idea
Phil-Garmann commentedon Mar 19, 2026
It seems like the this was implemented with #1296
MCRE-BE commentedon Mar 20, 2026
@Phil-Garmann : thanks for figuring that one out.
I got it working
Pixelements commentedon May 26, 2026
Could you post the schema.yml definition of your primary_key constraint definition? I can't get it working. I've tried "expression: rely" & "expression: "rely". Thank you so much!
MCRE-BE commentedon May 27, 2026
For Primary Key's
For Foriegn Key's
So I guess it's case sensitive?
Phil-Garmann commentedon May 27, 2026
@Pixelements my bad; this was only partly solved by #1296...
Phil-Garmann commentedon Jul 31, 2026
@sd-db any chance that this will be picked up soon? 🤞
sd-db commentedon Aug 13, 2026
@Phil-Garmann I had an initial PR ready, but currently busy with core-v2 work. Since this is a large change, it would take time (probably next month)
Phil-Garmann commentedon Sep 11, 2026
@sd-db any news? And would the changes be included in v2/fusion as well? 👀
ivanvyd commentedon Sep 25, 2026
One data point on the "QoL safeguard" in the original proposal, which may matter for the PR in progress: on Databricks, dbt's built-in
uniquetest does not detect duplicates once the primary key is declaredRELY. A safeguard built on that test would pass on the data it is meant to catch.Repro (dbt-core 1.12.3, dbt-databricks 1.12.5, serverless SQL warehouse, DBSQL 2026.36, Photon)
Two table models select the same six rows from a source where
order_id = 3appears twice. Their contracts differ only in the PK option, set with theexpression: RELYworkaround from earlier in this thread:dbt build:Why: the test compiles to
group by order_id having count(*) > 1. WithRELY, the optimizer treatsorder_idas unique, so every group has one row, and it plans the query asLocalTableScan <empty>. Query history shows 0 rows and 0 files read. On a 1,000,010-row table (16 files, 10 duplicated keys), theNORELYrun found all 10 duplicate keys and theRELYrun found none. The docs sayRELYrewrites need Photon; I only tested on Photon.A window-based test still catches it. With this override in the project, the
RELYmodel's test fails with 2 rows:{% macro databricks__test_unique(model, column_name) %} select unique_field, n_records from ( select {{ column_name }} as unique_field, count(*) over (partition by {{ column_name }}) as n_records from {{ model }} where {{ column_name }} is not null ) counted where n_records > 1 {% endmacro %}What this means for the safeguard
uniquedata test that runs after the constraint exists, which is the normaldbt buildorder, passes on violating data.uniqueconstraint doesn't help either, because Databricks does not enforceUNIQUEconstraints.RELY, or have the adapter'suniquetest count with a window whenRELYis set. The window version held in my runs, but I found no documentation promising the optimizer will leave it alone.Write-up with the plans and query-history evidence: https://community.databricks.com/t5/community-articles/declaring-a-primary-key-rely-turned-my-dbt-unique-test-green-on/m-p/169864
Phil-Garmann commentedon Oct 19, 2026
@sd-db should I port this issue to the main
dbtrepo for better traceability for the v2 development?