Search code examples
dbt

dbt two argument ref


How do I refer to relative path from view1 to view2 using ref('package_name', 'model_name') ?

/root_folder
   / project1
     /models
       view1.sql
     dbt_project.yml

   / project2
     /models
       view2.sql
     dbt_project.yml

There is no code example in the documentation.

Thank you.


Solution

  • To try and answer the question with a little more focus on your comment:

    I have one folder for each dataset I have on bigquery. Can I write models for several datasets in one single folder?

    Yes you can!

    Quick note on terms from the dbt docs "BigQuery configurations" in case you are not using bigquery.

    • schema is interchangeable with the BigQuery concept dataset
    • database is interchangeable with the BigQuery concept of project

    Here is how this works for me:

    project-dir
         | analysis
         | data
         | macros
         | models
             |> sources
                  - dataset1.yml
                  - dataset2.yml
         | seed
         | dbt_project.yml
         | packages.yml
    

    Where the contents of a dataset.yml is:

    version: 2
    
    sources:
      - name: fivetran_log
        database: my-bigquery-project-id
        loader: fivetran
        
        tables:
          - name: account
          - name: log
          - name: user
    

    No references are required within the dbt_project.yml to utilize these sources immediately. Instead, you can reference this directly from models like:

    select * 
    from {{ source('fivetran_log', 'user') }}
    

    That should allow you to have multiple dataset sources, but one single dbt project directory for all your views.

    However, if the datasets you are referencing are within different bigquery regions or different billing projects, I believe you will run into some errors.


    Appendix of related questions / resources across the dbt-verse: