get_relation returns none in hook context
See original GitHub issueDescribe 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:
- Created 3 years ago
- Reactions:1
- Comments:9 (9 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That is super helpful context and I think it gives me my path forward. Thanks so much!
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:
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.