Enabling Jinja syntax support in SQL files in PyCharm

It turns out that you can add Jinja syntax support for dbt model files (.sql) and yaml files in PyCharm Professional Edition or other paid JetBrains products that have multi-language support (Sorry DataGrip users). Here’s how to do it.

Disclaimers

  1. Unfortunately this is not available in the Community Edition of PyCharm.
  2. There is currently a bug in JetBrains IDE that shows a valid SQL with Jinja as having syntax errors if a SQL Dialect is selected to something other than “Generic SQL” (e.g Snowflake) Link to ticket. The workaround is to either set the global or project SQL Dialect setting under Languages & Frameworks to “Generic SQL”.
  3. This is not possible to do in DataGrip

Steps

  1. Preferences > Languages and Frameworks > Template Languages.
  2. Select “Jinja2” for the Template Language dropdown at the top,
  3. Click on the “+” icon to add SQL (and YAML) to the list. Hit Apply or OK and you are done!

4 Likes

I just recently rolled out a dbt workstation environment to a team of 15 and I have been asked if this sort of configuration is possible using VSCode. The VSCode python extension behaves properly and locates the dbt virtualenv, but I ly by manually initiating the extension. Since it is only set to auto initialize on .py files…not .sql

Does anyone know if this is possible in VSCode?

Re VS Code you might want to check this out to see if it suits your use case.

1 Like

Thanks, I will take a look at it.

I’ve just commented on the ticket you raised as it has been conflated with the DataGrip ticket and flagged as a duplicate which it patently isn’t.

I was trying to follow your steps but whenever I setup jinja2 it affects my keyboard and i am not able to type anything anymore in my IDE (Pycharm pro), anyone experienced something similar ?

Thanks

2 Likes

I am experiencing the same thing in PyCharm. I am able to set up the .sql files to have templates but when I specify the template language as Jinja, all my keyboard inputs are eaten up.

3 Likes

Same issue for me using PyCharm 2023.2.1 (Professional Edition).
Does someone has a solution?

1 Like