Skip to content

Commit

Permalink
add relation examples for {{this}} (#4011)
Browse files Browse the repository at this point in the history
this pr expand options for {{this}} and use keywords/related docs to
improve linkage. This was flagged by @alison985 in the [dbt slack
community](https://getdbt.slack.com/archives/C0441GSRU04/p1692113239216959).




Resolves #3939
  • Loading branch information
mirnawong1 authored Sep 19, 2023
2 parents cb54bed + c0a48a4 commit 84b0927
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions website/docs/reference/dbt-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ These classes are often useful when building advanced dbt models and macros.

The `Relation` object is used to interpolate schema and <Term id="table" /> names into SQL code with appropriate quoting. This object should _always_ be used instead of interpolating values with `{{ schema }}.{{ table }}` directly. Quoting of the Relation object can be configured using the [`quoting` config](/reference/project-configs/quoting).


### Creating relations

A `Relation` can be created by calling the `create` class method on the `Relation` class.
Expand All @@ -32,6 +33,7 @@ class Relation:

### Using relations

In addition to `api.Relation.create`, dbt returns a Relation when you use [`ref`](/reference/dbt-jinja-functions/ref), [`source`](/reference/dbt-jinja-functions/source) or [`this`](/reference/dbt-jinja-functions/this).
<File name='relation_usage.sql'>

```jinja2
Expand Down
1 change: 1 addition & 0 deletions website/docs/reference/dbt-jinja-functions/ref.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ from {{ref('model_a')}}
`ref()` is, under the hood, actually doing two important things. First, it is interpolating the schema into your model file to allow you to change your deployment schema via configuration. Second, it is using these references between models to automatically build the dependency graph. This will enable dbt to deploy models in the correct order when using `dbt run`.

The `{{ ref }}` function returns a `Relation` object that has the same `table`, `schema`, and `name` attributes as the [{{ this }} variable](/reference/dbt-jinja-functions/this).
- Note &mdash; Prior to dbt v1.6, the dbt Cloud IDE returns `request` as the result of `{{ ref.identifier }}`.

## Advanced ref usage

Expand Down
1 change: 1 addition & 0 deletions website/docs/reference/dbt-jinja-functions/source.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This function:
- Creates dependencies between a source and the current model, which is useful for documentation and model selection
- Compiles to the full object name in the database


## Related guides
- [Using sources](/docs/build/sources)

Expand Down
11 changes: 10 additions & 1 deletion website/docs/reference/dbt-jinja-functions/this.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ title: "about this"
sidebar_label: "this"
id: "this"
description: "Represents the current model in the database."
keywords:
- relation, relation object, this function, this jinja, this.database, this.schema, this.identifier
meta:
label: 'this'
---

`this` is the database representation of the current model. It is useful when:
- Defining a `where` statement within [incremental models](/docs/build/incremental-models)
- Using [pre or post hooks](/reference/resource-configs/pre-hook-post-hook)

`this` is a [Relation](/reference/dbt-classes#relation), and as such, properties such as `{{ this.database }}` and `{{ this.schema }}` compile as expected.
`this` is a [Relation](/reference/dbt-classes#relation), and as such, properties such as `{{ this.database }}` and `{{ this.schema }}` compile as expected.
- Note &mdash; Prior to dbt v1.6, the dbt Cloud IDE returns `request` as the result of `{{ ref.identifier }}`.

`this` can be thought of as equivalent to `ref('<the_current_model>')`, and is a neat way to avoid circular dependencies.

Expand Down Expand Up @@ -54,3 +59,7 @@ from raw_app_data.events
```

</File>




0 comments on commit 84b0927

Please sign in to comment.