Peewee: How to print raw sql for Blog.select().where(Blog.id == 1)

Created on 29 Nov 2019  路  8Comments  路  Source: coleifer/peewee

q = Blog.select().where(Blog.id == 1)
print(q.sql())

I want to compile to raw sql .

All 8 comments

Peewee returns a parameterized query, a 2-tuple of SQL string and parameters. This is to save people doing crazy stuff and getting SQL injection vulnerabilities. The driver accepts this 2-tuple and does the right thing.

What about for DB API (PEP-249) v2.0 drivers? How can I tell peewee to not add double quotes on everything?
ex:
from: SELECT "t1"."item1", "t1"."item2", "t1"."item3" FROM "trex_inventory_recipe_material" AS t1
to: SELECT t1.item1, t1.item2, t1.item3 FROM trex_inventory_recipe_material AS t1

Why would you want to do that? Lots of times people unwittingly (or intentionally) use reserved words for column-names, e.g. "count". Peewee adds quotes to ensure that identifiers are always treated as-such.

I'm using Impyla which is fully DB API v2.0 compiant. And the HiveServer2 (Hive) gives an error when it sees double quoted column-names.

On your database class, just set the quote attribute to be an empty string.

class HiveDatabase(...)
    quote = ''

Does that address the issue?

No change - using peewee 2.9.0
I'm still getting double quotes when doing a select() on the Model,

SELECT "t1"."rname", "t1"."mrole", "t1"."rmaterial" FROM "db1"."item_table" AS t1 []

peewee 2.9.0

That's a very old version, from March 2017. For peewee 3.0, I think you would actually set quote = ('', '') instead of empty string, but since you're on such an old version I'm not sure what to advise.

Thanks @coleifer - appreciate it. Thanks for pointing me in the right path - using quote = ''
Also, you reminded me that I forgot to change the version on the documentation. I was looking at the "latest". Older versions (like 2.9.0) use quote_char = ''
Yup; I will be upgrading it and fix any breaks along the way. Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kalimalrazif picture kalimalrazif  路  3Comments

alexpantyukhin picture alexpantyukhin  路  5Comments

GMaxera picture GMaxera  路  4Comments

mikemill picture mikemill  路  3Comments

ezk84 picture ezk84  路  4Comments