dbt-postgres模型生成表时schema重复添加public前缀问题
dbt-postgres 表 Schema 重复问题解决
问题现象
本地搭建dbt-postgres测试环境时,创建的表出现Schema重复:
- 期望生成:
public.test - 实际生成:
public_public.test
即使在模型中指定其他Schema,也会生成带public_前缀的新Schema。
相关配置文件
模型文件 test.sql
{{ config(materialized='table', schema='public') }} select a from table x
dbt_project.yml(原标注文件名dbt_profile.yml应为dbt_project.yml)
name: 'abc' version: '0.1' config-version: 2 profile: 'abc' model-paths: ["models"] analysis-paths: ["analyses"] test-paths: ["tests"] seed-paths: ["seeds"] macro-paths: ["macros"] snapshot-paths: ["snapshots"] target-path: "target" clean-targets: - "target" - "dbt_packages" - "dbt_modules" - "logs" models: abc: materialized: table
profiles.yml
abc: outputs: dev: type: postgres threads: 1 host: "localhost" port: 5432 user: "admin" pass: "admin" dbname: database schema: "public" target: dev
问题原因与解决方法
核心原因
dbt默认会将profiles.yml中指定的目标Schema与模型配置里的Schema进行拼接。你同时在两处指定了public,就会出现public_public的重复结果。
解决方案
场景1:所有模型默认使用public Schema
- 直接删除模型文件中的
schema='public'配置,dbt会自动使用profiles.yml中指定的publicSchema,生成的表即为public.test。 - 若要统一配置所有模型的Schema,也可在
dbt_project.yml的models块中添加schema: public:models: abc: materialized: table schema: public
场景2:部分模型需指定不同Schema
- 移除
profiles.yml中dev输出里的schema: "public"配置(或改为你的默认基础Schema,比如analytics),之后在需要指定Schema的模型中单独配置schema='public',此时dbt不会拼接前缀,直接使用模型指定的Schema。
内容的提问来源于stack exchange,提问作者Dametime
相关产品推荐
相关产品推荐

