0

I'm using the Airflow PostgresOperator with argument parameters in order to replace the table name of my sql query with the one contained in my dictionary. For example:

create_table = PostgresOperator(sql='DROP TABLE if exists %(my_table)s;',
                                parameters={'my_table':'my_name'},...)

Problem is when operator is executed, the rendered sql is DROP TABLE if exists 'my_name' and not DROP TABLE if exists my_name as expected (and of course this operation fails).

How to force Airflow not to put single quotes around rendered name?

Patrick
  • 2,577
  • 6
  • 30
  • 53

1 Answers1

1

Try using params instead of parameters, should work because sql is declared as a templated field in the PostgresOperator.

create_table = PostgresOperator(sql="DROP TABLE if exists {{ params.my_table }};",
                                params={'my_table':'my_name'},...)

I haven't test it, but there is a similar use case in the providers-package documentation.

NicoE
  • 4,373
  • 3
  • 18
  • 33
  • Frankly I don't see why there are params and parameters. Anyway it's working with params, thank you ! – Patrick Dec 05 '21 at 00:05
  • Hey @Patrick Glad to hear that it worked! `params` comes from `BaseOperator` so it's accesible pretty much anywhere, also works when triggering DAGs from the UI, here is an [example](https://stackoverflow.com/a/68107775/10569220). – NicoE Dec 05 '21 at 12:58