I’ve having quite a lot of trouble running seemingly valid Jinja code.
The problem I’m having
When I try to return the first element in a list or access data from the model’s schema - it appears to contain the data for printing, but not accessing.
The context of why I’m trying to do this
Context 1) Retrieve a column meeting certain criteria in the schema.
Context 2) Access a field’s value from the model’s schema and use it to reference a model.
What I’ve already tried (simplified example which exhibits the same issue)
Example 1)
{%- set column = model.columns.values() | first -%}
{{ column }} /* Works, displaying an object "{'name': '_object_key', 'description'..." */
{{ column.name }} /* Errors only if it's within in a macro */
Example 2)
{{model.meta.raw_model_name }} /* Works, prints out full string */
from {{ ref(model.meta.raw_model_name) }} /* Errors, in both model and macro, seems to be undefined. Writing the same result as printed out above as a string does work. */
Some example code or error messages
Error 1)
Compilation Error in model refined_…(models\Refined\refined_…sql)
No first item, sequence was empty.in macro transform_… (macros\transform_…)
called by model refined_… (models\Refined\wms_ctd\refined_…sql)
Error 2)
Compilation Error in model refined_… (models\Refined\wms_ctd\refined_…sql)
The name argument to ref() must be a string, got <class ‘jinja2.runtime.Undefined’>in macro transform_… (macros\transform_…sql)
called by model refined_wms_ctd_dbo_pckwrk_dtl (models\Refined\refined_…sql)
Are there some Jinja limitations I should be aware of, or have I stumbled upon a bug? Both of my issues seem likely to share the same root cause.