如何在dbt中测试种子(seed)与外部source的关系?
解决dbt种子与外部Source关系测试报错的方法
问题原因
报错'test_relationship' is undefined通常是因为dbt版本过低——dbt-core v0.17.0及以上才支持对Seed表使用内置的relationship测试,旧版本无此能力,会识别不了该宏。
可行解决方法
1. 升级dbt-core到最新稳定版
执行命令升级:
pip install --upgrade dbt-core
升级后重新运行测试:
dbt test --select seeds.products_market
2. 确认YAML配置的缩进正确性
YAML对空格缩进严格,检查seeds/schema.yml的格式是否完全合规,正确示例:
version: 2 seeds: - name: products_market description: "" columns: - name: id_source description: "ID source" - name: id_target description: "id target" tests: - relationship: to: source('dt', 'products') field: id_product
3. 自定义测试宏(无法升级dbt版本时用)
若受环境限制不能升级版本,可在macros/目录下创建自定义关系测试宏test_relationship.sql:
{% macro test_relationship(model, column_name, to, field) %} select {{ column_name }} from {{ model }} where {{ column_name }} not in ( select {{ field }} from {{ to }} ) {% endmacro %}
保持原schema.yml配置不变,运行测试即可。
验证效果
运行测试命令后,若id_target存在不在dt.products.id_product中的值,测试会失败并列出不符合的数据;全部匹配则测试通过。
内容的提问来源于stack exchange,提问作者Alex
相关产品推荐
相关产品推荐

