1 ==========================
2 Performing raw SQL queries
3 ==========================
5 .. currentmodule:: django.db.models
7 When the :doc:`model query APIs </topics/db/queries>` don't go far enough, you
8 can fall back to writing raw SQL. Django gives you two ways of performing raw
9 SQL queries: you can use :meth:`Manager.raw()` to `perform raw queries and
10 return model instances`__, or you can avoid the model layer entirely and
11 `execute custom SQL directly`__.
13 __ `performing raw queries`_
14 __ `executing custom SQL directly`_
16 .. _executing-raw-queries:
18 Performing raw queries
19 ======================
23 The ``raw()`` manager method can be used to perform raw SQL queries that
24 return model instances:
26 .. method:: Manager.raw(raw_query, params=None, translations=None)
28 This method method takes a raw SQL query, executes it, and returns a
29 :class:`~django.db.models.query.RawQuerySet` instance. This
30 :class:`~django.db.models.query.RawQuerySet` instance can be iterated
31 over just like an normal QuerySet to provide object instances.
33 This is best illustrated with an example. Suppose you've got the following model::
35 class Person(models.Model):
36 first_name = models.CharField(...)
37 last_name = models.CharField(...)
38 birth_date = models.DateField(...)
40 You could then execute custom SQL like so::
42 >>> for p in Person.objects.raw('SELECT * FROM myapp_person'):
47 Of course, this example isn't very exciting -- it's exactly the same as
48 running ``Person.objects.all()``. However, ``raw()`` has a bunch of other
49 options that make it very powerful.
51 .. admonition:: Model table names
53 Where'd the name of the ``Person`` table come from in that example?
55 By default, Django figures out a database table name by joining the
56 model's "app label" -- the name you used in ``manage.py startapp`` -- to
57 the model's class name, with an underscore between them. In the example
58 we've assumed that the ``Person`` model lives in an app named ``myapp``,
59 so its table would be ``myapp_person``.
61 For more details check out the documentation for the
62 :attr:`~Options.db_table` option, which also lets you manually set the
67 No checking is done on the SQL statement that is passed in to ``.raw()``.
68 Django expects that the statement will return a set of rows from the
69 database, but does nothing to enforce that. If the query does not
70 return rows, a (possibly cryptic) error will result.
72 Mapping query fields to model fields
73 ------------------------------------
75 ``raw()`` automatically maps fields in the query to fields on the model.
77 The order of fields in your query doesn't matter. In other words, both
78 of the following queries work identically::
80 >>> Person.objects.raw('SELECT id, first_name, last_name, birth_date FROM myapp_person')
82 >>> Person.objects.raw('SELECT last_name, birth_date, first_name, id FROM myapp_person')
85 Matching is done by name. This means that you can use SQL's ``AS`` clauses to
86 map fields in the query to model fields. So if you had some other table that
87 had ``Person`` data in it, you could easily map it into ``Person`` instances::
89 >>> Person.objects.raw('''SELECT first AS first_name,
90 ... last AS last_name,
93 ... FROM some_other_table''')
95 As long as the names match, the model instances will be created correctly.
97 Alternatively, you can map fields in the query to model fields using the
98 ``translations`` argument to ``raw()``. This is a dictionary mapping names of
99 fields in the query to names of fields on the model. For example, the above
100 query could also be written::
102 >>> name_map = {'first': 'first_name', 'last': 'last_name', 'bd': 'birth_date', 'pk': 'id'}
103 >>> Person.objects.raw('SELECT * FROM some_other_table', translations=name_map)
108 ``raw()`` supports indexing, so if you need only the first result you can
111 >>> first_person = Person.objects.raw('SELECT * from myapp_person')[0]
113 However, the indexing and slicing are not performed at the database level. If
114 you have a big amount of ``Person`` objects in your database, it is more
115 efficient to limit the query at the SQL level::
117 >>> first_person = Person.objects.raw('SELECT * from myapp_person LIMIT 1')[0]
119 Deferring model fields
120 ----------------------
122 Fields may also be left out::
124 >>> people = Person.objects.raw('SELECT id, first_name FROM myapp_person')
126 The ``Person`` objects returned by this query will be deferred model instances
127 (see :meth:`~django.db.models.query.QuerySet.defer()`). This means that the
128 fields that are omitted from the query will be loaded on demand. For example::
130 >>> for p in Person.objects.raw('SELECT id, first_name FROM myapp_person'):
131 ... print p.first_name, # This will be retrieved by the original query
132 ... print p.last_name # This will be retrieved on demand
137 From outward appearances, this looks like the query has retrieved both
138 the first name and last name. However, this example actually issued 3
139 queries. Only the first names were retrieved by the raw() query -- the
140 last names were both retrieved on demand when they were printed.
142 There is only one field that you can't leave out - the primary key
143 field. Django uses the primary key to identify model instances, so it
144 must always be included in a raw query. An ``InvalidQuery`` exception
145 will be raised if you forget to include the primary key.
150 You can also execute queries containing fields that aren't defined on the
151 model. For example, we could use `PostgreSQL's age() function`__ to get a list
152 of people with their ages calculated by the database::
154 >>> people = Person.objects.raw('SELECT *, age(birth_date) AS age FROM myapp_person')
156 ... print "%s is %s." % (p.first_name, p.age)
161 __ http://www.postgresql.org/docs/8.4/static/functions-datetime.html
163 Passing parameters into ``raw()``
164 ---------------------------------
166 If you need to perform parameterized queries, you can use the ``params``
167 argument to ``raw()``::
170 >>> Person.objects.raw('SELECT * FROM myapp_person WHERE last_name = %s', [lname])
172 ``params`` is a list of parameters. You'll use ``%s`` placeholders in the
173 query string (regardless of your database engine); they'll be replaced with
174 parameters from the ``params`` list.
178 **Do not use string formatting on raw queries!**
180 It's tempting to write the above query as::
182 >>> query = 'SELECT * FROM myapp_person WHERE last_name = %s' % lname
183 >>> Person.objects.raw(query)
187 Using the ``params`` list completely protects you from `SQL injection
188 attacks`__, a common exploit where attackers inject arbitrary SQL into
189 your database. If you use string interpolation, sooner or later you'll
190 fall victim to SQL injection. As long as you remember to always use the
191 ``params`` list you'll be protected.
193 __ http://en.wikipedia.org/wiki/SQL_injection
195 .. _executing-custom-sql:
197 Executing custom SQL directly
198 =============================
200 Sometimes even :meth:`Manager.raw` isn't quite enough: you might need to
201 perform queries that don't map cleanly to models, or directly execute
202 ``UPDATE``, ``INSERT``, or ``DELETE`` queries.
204 In these cases, you can always access the database directly, routing around
205 the model layer entirely.
207 The object ``django.db.connection`` represents the
208 default database connection, and ``django.db.transaction`` represents the
209 default database transaction. To use the database connection, call
210 ``connection.cursor()`` to get a cursor object. Then, call
211 ``cursor.execute(sql, [params])`` to execute the SQL and ``cursor.fetchone()``
212 or ``cursor.fetchall()`` to return the resulting rows. After performing a data
213 changing operation, you should then call
214 ``transaction.commit_unless_managed()`` to ensure your changes are committed
215 to the database. If your query is purely a data retrieval operation, no commit
216 is required. For example::
219 from django.db import connection, transaction
220 cursor = connection.cursor()
222 # Data modifying operation - commit required
223 cursor.execute("UPDATE bar SET foo = 1 WHERE baz = %s", [self.baz])
224 transaction.commit_unless_managed()
226 # Data retrieval operation - no commit required
227 cursor.execute("SELECT foo FROM bar WHERE baz = %s", [self.baz])
228 row = cursor.fetchone()
232 If you are using more than one database you can use
233 ``django.db.connections`` to obtain the connection (and cursor) for a
234 specific database. ``django.db.connections`` is a dictionary-like
235 object that allows you to retrieve a specific connection using its
238 from django.db import connections
239 cursor = connections['my_db_alias'].cursor()
241 transaction.commit_unless_managed(using='my_db_alias')
243 By default, the Python DB API will return results without their field
244 names, which means you end up with a ``list`` of values, rather than a
245 ``dict``. At a small performance cost, you can return results as a
246 ``dict`` by using something like this::
248 def dictfetchall(cursor):
249 "Returns all rows from a cursor as a dict"
250 desc = cursor.description
252 dict(zip([col[0] for col in desc], row))
253 for row in cursor.fetchall()
256 Here is an example of the difference between the two::
258 >>> cursor.execute("SELECT id, parent_id from test LIMIT 2");
259 >>> cursor.fetchall()
260 ((54360982L, None), (54360880L, None))
262 >>> cursor.execute("SELECT id, parent_id from test LIMIT 2");
263 >>> dictfetchall(cursor)
264 [{'parent_id': None, 'id': 54360982L}, {'parent_id': None, 'id': 54360880L}]
267 .. _transactions-and-raw-sql:
269 Transactions and raw SQL
270 ------------------------
272 When you make a raw SQL call, Django will automatically mark the
273 current transaction as dirty. You must then ensure that the
274 transaction containing those calls is closed correctly. See :ref:`the
275 notes on the requirements of Django's transaction handling
276 <topics-db-transactions-requirements>` for more details.
278 .. versionchanged:: 1.3
280 Prior to Django 1.3, it was necessary to manually mark a transaction
281 as dirty using ``transaction.set_dirty()`` when using raw SQL calls.
283 Connections and cursors
284 -----------------------
286 ``connection`` and ``cursor`` mostly implement the standard Python DB-API
287 described in :pep:`249` (except when it comes to :doc:`transaction handling
288 </topics/db/transactions>`). If you're not familiar with the Python DB-API, note
289 that the SQL statement in ``cursor.execute()`` uses placeholders, ``"%s"``,
290 rather than adding parameters directly within the SQL. If you use this
291 technique, the underlying database library will automatically add quotes and
292 escaping to your parameter(s) as necessary. (Also note that Django expects the
293 ``"%s"`` placeholder, *not* the ``"?"`` placeholder, which is used by the SQLite
294 Python bindings. This is for the sake of consistency and sanity.)