DBT中如何在文件夹内的模型里引用src.yml?
问题:DBT模型引用src.yml定义的源表失败
目录结构
- dataservice - dbt_model - models - my_folder - model_1.sql - src.yml - dbt_project.yml
src.yml内容
version: 2 sources: - name: Green schema: HistoryGreen tables: - name: trans
尝试过的无效代码
select * FROM {{ ref ('Green', 'trans') }} select * FROM {{ source ('Green', 'trans') }}
问题分析与解决
1. 纠正基础用法错误
{{ ref('Green', 'trans') }}用法错误:ref函数仅用于引用DBT自身生成的模型文件,不能用来调用src.yml里定义的源表,这个写法从一开始就不适用。{{ source('Green', 'trans') }}语法正确,但需要排查以下问题:
2. 排查要点
- 拼写与大小写检查:确认代码中
Green、trans的拼写和大小写,与src.yml中的定义完全一致(部分数据库对大小写敏感,大小写不匹配会导致找不到表)。 - 验证源定义是否被加载:运行命令
dbt source list,如果输出中能看到Green.trans,说明源定义已被DBT识别;如果看不到,检查dbt_project.yml中是否配置了正确的source-paths(默认models目录会被加载,可手动添加source-paths: ["models"]确保识别)。 - 数据库权限验证:确保DBT连接数据库使用的账号,拥有
HistoryGreenschema下trans表的SELECT权限。 - 确认dbt_project.yml配置:检查项目配置文件中是否正确设置了核心配置,示例如下:
name: 'dbt_model' version: '1.0.0' config-version: 2 profile: 'your_profile_name' model-paths: ["models"] source-paths: ["models"]
正确代码示例
select * from {{ source('Green', 'trans') }}
内容的提问来源于stack exchange,提问作者Abiodun Adeoye
相关产品推荐
相关产品推荐

