﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
25212	Document RawSQL expression	Tim Graham	Tim Graham	"from Anssi [https://groups.google.com/d/msg/django-developers/FojuU0syO8Y/YjUFY1jqCAAJ on django-developers]: 

 There doesn't seem to be direct documentation about RawSQL expression. The RawSQL is once mentioned in the documentation, but it definitely needs a bit more in the database functions documentation.
 [[BR]]
 The usage of RawSQL is really simple: `qs.annotate(val=RawSQL(""(select col from sometable where othercol= %s)"", (someparam,)))`
 [[BR]]
 This is equivalent to `qs.extra(select={'val': ""select col from sometable where othercol = %s""}, select_params=(someparam,))`
 [[BR]]
 The main benefit of using RawSQL is that you can set output_field if needed. The main downside is that if you refer to some table alias of the qs in the raw sql, then it is possible that Django changes that alias (for example, when the qs is used as a subquery in yet another query).
 [[BR]]
 Combining raw SQL with alias relabeling support is possible, too with the following library: https://github.com/akaariai/django-refsql"	Cleanup/optimization	closed	Documentation	1.8	Normal	fixed			Accepted	1	0	0	0	0	0
