"target is undefined" 运行 dbt 时出错

"target is undefined" error when running 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。注释掉该行后,它工作正常。

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

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

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

参考this

你的 post-hook 实际上应该是这样的:

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

on-run-end context 的 dbt 文档对此进行了详细解释,但长话短说:因为 dbt 运行 可能会触及目标数据库上不同模式中的 tables,所以不是可以应用 grant 语句的单个 target.schema 值。相反,上下文为您提供了一个名为 schemas 的模式名称列表,您需要循环访问它们。该列表包含一个或多个元素。

dbt 中的 target 是适配器的配置数据,如帐户、用户、端口或架构。 this是关于正在写入的数据库对象,还包括一个字段schema。最后,on-run-end 上下文提供了模式列表,这样您就不必为每个 table 或视图进行冗余的授权声明,而可以为每个模式只进行一次授权。