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

DBT使用宏设置变量报错:'None' has no attribute 'table'

问题排查与解决方案

报错原因分析

报错'None' has no attribute 'table'的核心原因是source函数调用语法错误:
你调用source('raw.event','tmp')的写法不符合dbt规范,source函数第一个参数应为source定义名称(对应sources.yml/dbt_project.yml里的name字段),第二个参数是表名,而非schema.table格式。错误调用会返回None,宏解析{{ target_table }}时就会触发该报错。

修复步骤

1. 修正source调用语法

先确认你的sources定义(示例):

sources:
  - name: raw  # source名称
    schema: raw  # BigQuery中的schema
    tables:
      - name: tmp  # 目标表名

然后将模型中的调用修正为:

{% set dates = get_last_two_dates('window_start', source('raw', 'tmp')) %}

2. 增强宏的健壮性

原宏存在无数据时的报错风险,修改后代码:

{% macro get_last_two_dates(target_column_name, target_table = this) %}
{% set query %}
select string_agg(format('%T', target_date), ',') target_date_string
from (
    SELECT distinct date({{ target_column_name }}) target_date
    FROM {{ target_table }}
    order by target_date desc
    LIMIT 2
) a
{% endset %}

{% set result = run_query(query) %}
{% if result and result.rows %}
    {% do return(result.columns[0][0]) %}
{% else %}
    {% do return("''") %}  # 无数据时返回空字符串,避免后续SQL报错
{% endif %}
{% endmacro %}

3. 增量加载SQL示例(BigQuery适配)

获取日期后,可编写insert_overwrite逻辑:

{{ config(
    materialized='incremental',
    incremental_strategy='insert_overwrite',
    partition_by={
        "field": "window_start_date",
        "data_type": "date"
    }
) }}

with filtered_data as (
    select *
    from {{ source('raw', 'tmp') }}
    where date(window_start) in ({{ dates }})
)

select *
from filtered_data

{% if is_incremental() %}
    where date(window_start) in ({{ dates }})
{% endif %}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 07:11:05