Search code examples
jinja2dbt

DBT macro ignores leading if block


I defined a macro that starts with:

{% macro my_macro(table, first=false) %}

{%- if first -%} WITH
{%- else -%} ,
{% endif %}

tmp AS (...

In the compiled SQL I see this is completely ignored. If I paste this exact same block after the first CTE, it inserts fine. If I put any text before the if-block, it will also render the text + content of if-block.

I don't understand this behaviour. How can I make this macro work leading either with WITH or ","?

Even if I replace first with true it doesn't show up.

Additional information: I'm working with BigQuery and dbt version 0.18.2


Solution

  • After trial and error I realised that using "-" to reduce white space on the first line somehow removes the text coming after it as well. So

    {%- if first -%}
    WITH
    ...
    

    Doesn't work, but

    {% if first %}
    WITH
    ...
    

    is fine. It was a simple fix, but I doubt this is expected behaviour.