dbt pre_hook新语法咨询:原写法触发弃用警告
dbt模型内pre_hook配置弃用警告的解决方法
问题背景
在模型自身的config段中配置pre_hook(而非放入project yml文件),此前运行正常,近期触发弃用错误,调整至meta后又出现解析错误。
原config配置
{{ config( materialized='incremental', incremental_strategy='append', alias='stg_' ~ company_code ~ '_sales', pre_hook= "{% if is_incremental() %} delete from {{ this }} where sale_date in ( select distinct sale_date from {{ source('" ~ company_code ~ "', 'Sales_Detail') }} where sync_date > (select max(sync_date) from {{ this }}) ) {% endif %}" ) }}
触发的弃用警告
[WARNING][CustomKeyInConfigDeprecation]: Deprecated functionality Custom key `pre-hook` found in `config` in file `models/marts/eagle_summaries/int_eagle_gaap_trial_balance.sql`. Custom config keys should move into the `config.meta`.
尝试的错误写法(移至meta)
meta: pre_hook: "{% if is_incremental() %} delete from {{ this }} where sale_date in ( select distinct sale_date from {{ source('" ~ company_code ~ "', 'Sales_Detail') }} where sync_date > (select max(sync_date) from {{ this }}) ) {% endif %}"
触发的解析错误
Parsing Error at path ['meta']: Undefined is not of type 'string'
正确解决方案
dbt新版本中,pre_hook(单数)已被弃用,需使用复数形式pre_hooks,且需用列表包裹,无需将其移入meta(meta用于存储自定义元数据,并非hook配置项)。
正确的config写法如下:
{{ config( materialized='incremental', incremental_strategy='append', alias='stg_' ~ company_code ~ '_sales', pre_hooks=[ "{% if is_incremental() %} delete from {{ this }} where sale_date in ( select distinct sale_date from {{ source('" ~ company_code ~ "', 'Sales_Detail') }} where sync_date > (select max(sync_date) from {{ this }}) ) {% endif %}" ] ) }}
说明
- 复数形式
pre_hooks是dbt当前推荐的配置方式,支持同时定义多个hook(列表内可添加多个字符串元素) - 无需将hook配置移入
meta,否则会因类型不匹配触发解析错误 - 原hook内的Jinja逻辑保持不变,仅需修改参数名并包裹为列表
内容的提问来源于stack exchange,提问作者Travis
相关产品推荐
相关产品推荐

