Skip to content
Showing with 4 additions and 0 deletions.
  1. +4 −0 peewee.py
View
4 peewee.py
@@ -2089,3 +2089,7 @@ def dfs(model):
for m in sorted(models, key=names, reverse=True):
dfs(m)
return list(reversed(ordering)) # want parents first in output ordering
+
+def raw_sql(query):
+ db = query.model_class._meta.database
+ return query.sql(db.get_compiler())

3 comments on commit a5ec7bb

@kevinisaac

Can someone please give an example of how to use this helper function?

@coleifer
Owner

query = MyModel.select()
print raw_sql(query)

@MarSoft

Because this page is one of first Google's results on the topic, I'll leave it here.
Here is how we can determine exact SQL produced by query, with all parameters substituted.
This works for PostgreSQL, but not sure about other database types.

query = MyModel.select().where(field1='value', fields2<datetime.datetime.now())
query.database.get_cursor().mogrify(*query.sql())
Please sign in to comment.
Something went wrong with that request. Please try again.