Search code examples
sqlpostgresqldbt

dbt treats referenced tables like columns and says they don't exist


The error

Database Error in model section2 (models\example\section2.sql)
column "section1" does not exist 
LINE 9: FROM ref(  section1  ) S1
                   ^

My project directory

/dbtxyz/models/example/section1.sql
/dbtxyz/models/example/section2.sql
/dbtxyz/project.yml
/dbtxyz/profile.yml

My profile.yml

default:
  outputs:
   dev:
     type: "postgres"
     host: "localhost"
     user: "postgres"
     password: "password"
     port: 5432
     dbname: "myprojectdata"
     schema: "myprojectdataschema"
  target: "dev"

My project.yml

analysis-paths:
- analyses
clean-targets:
- target
- dbt_packages
config-version: 2
macro-paths:
- macros
model-paths:
- models
models:
  dbtsample:
    example:
      +materialized: view
name: dbtxyz
profile: default
seed-paths:
- seeds
snapshot-paths:
- snapshots
test-paths:
- tests
vars:
  leverage: 123
version: 1.0.0

The section1.sql

{{ config(materialized='table') }}
{% set leverage= var('leverage') %}
WITH
section1 AS (
SELECT
T0.* FROM table0 T0
WHERE T0."leverage" > {{ leverage }}
)
SELECT * FROM section1

This is section2.sql

{{ config(materialized='table') }}

WITH
section2 AS (
SELECT
T1.*,
S1.*
FROM 
ref(  {{section1}}  ) S1 
JOIN table1 T1
ON S1."id" = T1."id"
)
SELECT * FROM section2

Because section1 is the first model in my chain of models, and now broken, it's backlogging everything.

This is the first time this has happened to me. I've never done dbt with postgresql before, so there's probably new things I'm not aware of.

  • The section1 table does in fact materialize in the myprojectdata database. If I view all tables in the database, it shows up.
  • I learned that unlike duckdb (what I usually work with for dbt), postgresql is case insensitive and seems to default every column name as lowercase unless explicitly framed in quotation marks. I thought maybe table names are also case sensitive, but even then, section1 is already lowercase. Nonetheless, I still enclosed the section1 in the first model with quotation marks, but it still gave the same error.

Has anyone gotten this before as well?


Solution

  • Your syntax in section2.sql is a bit tangled. Try:

    {{ ref('section1') }}