You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

dbt中在sources.yml用Jinja条件语句按环境排除表的问题

在dbt的sources.yml中根据环境条件排除源表的问题

我需要在uat-az环境运行模型tableX,该模型依赖tableC及其他表,但uat-az环境中没有tableC,所以希望在sources.yml里根据目标环境排除tableC。

我执行的命令:

dbt run --select tableX --target uat-az

修改后的sources.yml文件:

- name: sourceA
  schema: schemaA
  database: |
    {%- if "prod" in target.name  -%} raw_db_prod
    {%- else -%} raw_db_uat
    {%- endif -%}
  tables:
  - name: tableA
    description: The table A.
    freshness:
      warn_after: {count: 12, period: hour}
      error_after: {count: 24, period: hour}
    loaded_at_field: "to_timestamp(json:dt::string)"
    columns:
    - name: json
      description:  json.
- name: tableB
    description: The table B.
    columns:
    - name: dt
      description: the dt
    - name: sn
      description: the sn
    - name: payload

    {%- if target.name != "uat-az" and target.name != "prod-az" %}
  
- name: tableC
    description: The table C.
    columns:

    - name: _file
      description: the _file.
    - name: _line
      description: the _line.
     
    {% endif -%}

我尝试了多种Jinja空格控制配置,比如:

{%- if target.name != "uat-az" and target.name != "prod-az" %}
{% endif -%}
{%- if target.name != "uat-az" and target.name != "prod-az" -%}
{%- endif -%}

现有环境包括prod(主生产环境)、uat(主UAT环境)、prod-az(新生产环境)、uat-az(新UAT环境)。由于YAML对缩进要求严格,我始终没搞对,请问问题出在哪?


问题分析与解决方法

你的核心问题是YAML结构破坏和Jinja条件块位置错误,具体如下:

  1. tableB的缩进错误
    - name: tableB 作为独立的source条目,其下的description、columns等字段必须和name保持同级缩进(通常是2个空格),原代码里这些字段缩进过多,导致YAML本身就不合法。

  2. 条件块位置错误
    你把tableC的条件判断放在了tableB的条目内部,渲染后会导致tableB的YAML结构混乱。正确的做法是把条件块包裹整个tableC的source条目,放在和其他source条目(sourceA、tableB)同级的位置。

  3. Jinja空格控制优化
    使用{%- ... -%}移除块前后的空白,确保渲染后YAML没有多余空行或缩进错误,同时可以用target.name not in ["uat-az", "prod-az"]简化条件判断逻辑。

修正后的sources.yml:

- name: sourceA
  schema: schemaA
  database: |
    {%- if "prod" in target.name -%} raw_db_prod
    {%- else -%} raw_db_uat
    {%- endif -%}
  tables:
  - name: tableA
    description: The table A.
    freshness:
      warn_after: {count: 12, period: hour}
      error_after: {count: 24, period: hour}
    loaded_at_field: "to_timestamp(json:dt::string)"
    columns:
    - name: json
      description: json.
- name: tableB
  description: The table B.
  columns:
  - name: dt
    description: the dt
  - name: sn
    description: the sn
  - name: payload

{%- if target.name not in ["uat-az", "prod-az"] %}
- name: tableC
  description: The table C.
  columns:
  - name: _file
    description: the _file.
  - name: _line
    description: the _line.
{%- endif %}

如果tableC实际是属于某个source(比如sourceA)下的表,需要把条件块放在该source的tables列表内部,示例如下:

- name: sourceA
  schema: schemaA
  database: |
    {%- if "prod" in target.name -%} raw_db_prod
    {%- else -%} raw_db_uat
    {%- endif -%}
  tables:
  - name: tableA
    description: The table A.
    # tableA的其他配置...
  {%- if target.name not in ["uat-az", "prod-az"] %}
  - name: tableC
    description: The table C.
    columns:
    - name: _file
      description: the _file.
    - name: _line
      description: the _line.
  {%- endif %}

内容的提问来源于stack exchange,提问作者Gabriel Castaing

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 11:28:11