Skip to content

Wrap model selection in sub-select to handle WHERE clauses#103

Open
sti0 wants to merge 1 commit intoSnowflake-Labs:mainfrom
sti0:bugfix/constraint-test-subquery-where
Open

Wrap model selection in sub-select to handle WHERE clauses#103
sti0 wants to merge 1 commit intoSnowflake-Labs:mainfrom
sti0:bugfix/constraint-test-subquery-where

Conversation

@sti0
Copy link

@sti0 sti0 commented Sep 9, 2025

fixes

Example:

models:
 - name: table
   columns:
      - name: id
        data_tests:
          - dbt_constraints.foreign_key:
              pk_table_name: ref("parent_table")
              pk_column_name: id
              config:
                where: is_current
select validation_errors.* from (
    select
        fk_child.*
    from (
        select
            fk_child_inner.<<id>>
        from (select * from (select * from <<table>> where is_current) dbt_subquery) as fk_child_inner
        where 1=1
            and fk_child_inner.<<id>> is not null
            ) fk_child
    left join (
        select
            fk_parent_inner..<<id>>
        from <<parent_table>> fk_parent_inner
        ) fk_parent
            on fk_child.<<id>> = fk_parent.<<id>>

    where fk_parent.<<id>> is null
) validation_errors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant