How to read model columns in sql

The problem I’m having

I need to create a incremental model which queries itself. In this case I can utilize the is_incremental() to point to itself with {{ this }} if it the table is not created yet or for full-refresh.
Example:

, cteX as (
{% if is_incremental() %}
select
     *,
  from {{ this }} join ...
  where ...
{% else %}
with cte_table as (
  select
       cast(null as id) as int,
       cast(null as name) as string,
       ...
) select * from cte_table where false
{{ endif }}
),

Here I don’t want to hardcode all the columns but rather utilize the schema yml with model description on which the column and datatype are already mentioned. How can I do that?

What I’ve already tried

There is a utility available get_filtered_columns_in_relation which queries the table to fetch columns. But I want the columns to be fetched from yml description for that model. Because it will be the first run and the table won’t be available yet.

in dbt your should start with the query and then move to documentation, especially with AI that will take the types and write your documentation.

Note: @Nick Pinfold originally posted this reply in Slack. It might not have transferred perfectly.