question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

get_relation returns none in hook context

See original GitHub issue

Describe the bug

The adapter.get_relation(...) method seems to return None within the post-hook context.

Why does this matter? Well, {{ this }} always has its type defined as None, and it’s fairly common that a user would want to use get_relation (perform a cache lookup) to get its relational type for the purposes of templating a post hook:

alter {{ relation.type }} {{ relation }} ... do something ...

Steps To Reproduce

macros/say_hi.sql

{% macro say_hi(relation) %}

    {% set rel = adapter.get_relation(this.database, this.schema, this.name) %}
    
   select 'say hi to my two best friends: the {{ relation.type }} {{ relation }} and the {{ rel.type }} {{ rel }}'

{% endmacro %}

models/my_model.sql

{{ config(
    post_hook = say_hi(this)
) }}

{{ say_hi(this) }}

dbt.log

create view "jerco"."dbt_jcohen"."my_model__dbt_tmp" as (
    select 'say hi to my two best friends: the None "jerco"."dbt_jcohen"."my_model" and the view "jerco"."dbt_jcohen"."my_model"'
  );
...
        select 'say hi to my two best friends: the None "jerco"."dbt_jcohen"."my_model" and the  None'
...

Expected behavior

I expected the result of get_relation to be defined for both the model and post-hook contexts.

The output of dbt --version:

$ dbt --version
installed version: 0.18.1
   latest version: 0.18.1

Additional context

In the meantime, it’s mostly possible to work around this by inferring the relational object type from the configuration of the model we’re post-hooking:

{% set relation_type='view' if model.config.materialized == 'view' else 'table' %}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
aaronsteerscommented, Dec 11, 2020

That is super helpful context and I think it gives me my path forward. Thanks so much!

0reactions
jtcohen6commented, Jul 2, 2021

Coming back to this, and having revisited https://github.com/dbt-labs/dbt/issues/2370, I’m afraid to report that this does work if the hook is re-parsed at execute time, instead of set at parse time. Namely:

{{ config(
    post_hook = "{{ say_hi(this) }}"
) }}

{{ say_hi(this) }}

This is so not my favorite thing, but I’m going to close the issue. The real question is bigger than get_relation, and it’s whether hooks should always be re-parsed at execute time, without the need to nest a macro in an extra set of curlies.

Read more comments on GitHub >

github_iconTop Results From Across the Web

get_relation returns none in hook context · Issue #2938 - GitHub
Describe the bug The adapter.get_relation(...) method seems to return None within the post-hook context. Why does this matter?
Read more >
React Hook + Context api - Stack Overflow
I am fairly new to React Hooks and context api. I am trying to create a global state management within react (no redux)...
Read more >
Static Methods | Objection.js
The context object of query that produced the empty result. See context. props, any, Data passed to the error class constructor. # Return...
Read more >
A Guide to JUnit 5 Extensions - Baeldung
In this article, we're going to take a look at the extension model in the JUnit 5 testing library. As the name suggests,...
Read more >
12. Data access with JDBC - Spring
Application code is required to retrieve the JDBC connection through DataSourceUtils.getConnection(DataSource) instead of Java EE's standard DataSource.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found