运行dbt时出现“目标未定义”错误

时间:2020-02-11 22:45:07

标签: dbt

我有一个dbt_project.yml,例如:


name: rdb
profile: rdb
source-paths: ['models']
version: "0.1"

models:
  rdb:
    schema: cin
    materialized: table
    post-hook: 'grant select on {{ this }} to rer'
    on-run-end: 
        # TODO: fix
        - 'grant usage on schema "{{ target.schema }}" to rer'

DBT运行非常好。但是对于on-run-end条目,它将失败,并显示Compilation Error 'target' is undefined。注释掉该行后,它就可以正常工作。

我犯了一个基本错误吗?谢谢!

2 个答案:

答案 0 :(得分:2)

我的直觉是您不需要引用jinja模板。试试:

on-run-end:
    - 'grant usage on schema {{ target.schema }} to rer'

请参阅this以供参考。

答案 1 :(得分:2)

您的挂机实际上应如下所示:

on-run-end:
 - "{% for schema in schemas %}grant usage on schema {{ schema }} to rer;{% endfor %}"

on-run-end context的dbt文档对此进行了详细解释,但总而言之:由于dbt运行可能会触及目标数据库上不同模式的表,因此没有一个单独的target.schema值您可以应用拨款声明。相反,上下文为您提供了一个需要循环浏览的架构名称列表,称为schemas。该列表包含一个或多个元素。

dbt中的target是适配器的配置数据,例如帐户,用户,端口或架构。 this与正在写入的数据库对象有关,并且还包括字段schema。最后,on-run-end上下文提供了模式列表,因此您不必被迫对每个表或视图进行多余的授予语句,而只能对每个模式进行单个授予。

相关问题