sripathikrishnan / jinjasql

Template Language for SQL with Automatic Bind Parameter Extraction
MIT License
815 stars 89 forks source link

Substituting the same variable multiple times into #12

Closed notconfusing closed 4 years ago

notconfusing commented 6 years ago

Given the example query

drop table if exists {{population_table}};
create table {{population_table}} as....;

If I call: query, bind_params = j.prepare_query(templ, params_dict) and then engine.execute(query, bind_params) I get the error that there aren't enough bind_params to substitute into query. I believe this is because I've included the {{population_table}} variable twice in the template.

How could I go about fixing this? (It might be true that I should execute those queries separately, but it's also valid to have a case where there are several of the same substitution in one query).

henryrizzi commented 6 years ago

What kind of param style are you using? Have you tried looking at the query which is created from jinjasql?

devashishsharma2302 commented 6 years ago

@notconfusing JinjaSQL works perfectly well with multiple usages of the same variable in a single query. It assigns a random postfix to the key generated for same named parameter. In your case, JinjaSQL would have created two keys - population_table and poulation_table_456 (with some random number).

Can you please share the following details to track this issue down?

henryrizzi commented 6 years ago

@devashishsharma2302 The version of jinjasql which replaces with an underscore has not yet been pushed to pypi. On version 0.1.6 it's currently using # as a delimiter not _. Because of this jinjasql will fail when trying to execute in sqlalchemy using named parameters, as # is not considered a valid character in sqlalchemy bind parameters. I would guess that issue is happening here, since engine looks sqlalchemy like. 🤔

devashishsharma2302 commented 6 years ago

@henryrizzi Version 0.1.7 with the change is pushed to PyPi now. Thank you for reporting! @notconfusing Can you upgrade JinjaSQL to version 0.1.7 and check if the issue still persists?

sripathikrishnan commented 4 years ago

Closing this ticket as I believe the underlying issue has long been resolved.