1. ==========================
    
  2. Performing raw SQL queries
    
  3. ==========================
    
  4. 
    
  5. .. currentmodule:: django.db.models
    
  6. 
    
  7. Django gives you two ways of performing raw SQL queries: you can use
    
  8. :meth:`Manager.raw()` to `perform raw queries and return model instances`__, or
    
  9. you can avoid the model layer entirely and `execute custom SQL directly`__.
    
  10. 
    
  11. __ `performing raw queries`_
    
  12. __ `executing custom SQL directly`_
    
  13. 
    
  14. .. admonition:: Explore the ORM before using raw SQL!
    
  15. 
    
  16.     The Django ORM provides many tools to express queries without writing raw
    
  17.     SQL. For example:
    
  18. 
    
  19.     * The :doc:`QuerySet API </ref/models/querysets>` is extensive.
    
  20.     * You can :meth:`annotate <.QuerySet.annotate>` and :doc:`aggregate
    
  21.       </topics/db/aggregation>` using many built-in :doc:`database functions
    
  22.       </ref/models/database-functions>`. Beyond those, you can create
    
  23.       :doc:`custom query expressions </ref/models/expressions/>`.
    
  24. 
    
  25.     Before using raw SQL, explore :doc:`the ORM </topics/db/index>`. Ask on
    
  26.     one of :doc:`the support channels </faq/help>` to see if the ORM supports
    
  27.     your use case.
    
  28. 
    
  29. .. warning::
    
  30. 
    
  31.     You should be very careful whenever you write raw SQL. Every time you use
    
  32.     it, you should properly escape any parameters that the user can control
    
  33.     by using ``params`` in order to protect against SQL injection attacks.
    
  34.     Please read more about :ref:`SQL injection protection
    
  35.     <sql-injection-protection>`.
    
  36. 
    
  37. .. _executing-raw-queries:
    
  38. 
    
  39. Performing raw queries
    
  40. ======================
    
  41. 
    
  42. The ``raw()`` manager method can be used to perform raw SQL queries that
    
  43. return model instances:
    
  44. 
    
  45. .. method:: Manager.raw(raw_query, params=(), translations=None)
    
  46. 
    
  47. This method takes a raw SQL query, executes it, and returns a
    
  48. ``django.db.models.query.RawQuerySet`` instance. This ``RawQuerySet`` instance
    
  49. can be iterated over like a normal :class:`~django.db.models.query.QuerySet` to
    
  50. provide object instances.
    
  51. 
    
  52. This is best illustrated with an example. Suppose you have the following model::
    
  53. 
    
  54.     class Person(models.Model):
    
  55.         first_name = models.CharField(...)
    
  56.         last_name = models.CharField(...)
    
  57.         birth_date = models.DateField(...)
    
  58. 
    
  59. You could then execute custom SQL like so::
    
  60. 
    
  61.     >>> for p in Person.objects.raw('SELECT * FROM myapp_person'):
    
  62.     ...     print(p)
    
  63.     John Smith
    
  64.     Jane Jones
    
  65. 
    
  66. This example isn't very exciting -- it's exactly the same as running
    
  67. ``Person.objects.all()``. However, ``raw()`` has a bunch of other options that
    
  68. make it very powerful.
    
  69. 
    
  70. .. admonition:: Model table names
    
  71. 
    
  72.     Where did the name of the ``Person`` table come from in that example?
    
  73. 
    
  74.     By default, Django figures out a database table name by joining the
    
  75.     model's "app label" -- the name you used in ``manage.py startapp`` -- to
    
  76.     the model's class name, with an underscore between them. In the example
    
  77.     we've assumed that the ``Person`` model lives in an app named ``myapp``,
    
  78.     so its table would be ``myapp_person``.
    
  79. 
    
  80.     For more details check out the documentation for the
    
  81.     :attr:`~Options.db_table` option, which also lets you manually set the
    
  82.     database table name.
    
  83. 
    
  84. .. warning::
    
  85. 
    
  86.     No checking is done on the SQL statement that is passed in to ``.raw()``.
    
  87.     Django expects that the statement will return a set of rows from the
    
  88.     database, but does nothing to enforce that. If the query does not
    
  89.     return rows, a (possibly cryptic) error will result.
    
  90. 
    
  91. .. warning::
    
  92. 
    
  93.     If you are performing queries on MySQL, note that MySQL's silent type coercion
    
  94.     may cause unexpected results when mixing types. If you query on a string
    
  95.     type column, but with an integer value, MySQL will coerce the types of all values
    
  96.     in the table to an integer before performing the comparison. For example, if your
    
  97.     table contains the values ``'abc'``, ``'def'`` and you query for ``WHERE mycolumn=0``,
    
  98.     both rows will match. To prevent this, perform the correct typecasting
    
  99.     before using the value in a query.
    
  100. 
    
  101. Mapping query fields to model fields
    
  102. ------------------------------------
    
  103. 
    
  104. ``raw()`` automatically maps fields in the query to fields on the model.
    
  105. 
    
  106. The order of fields in your query doesn't matter. In other words, both
    
  107. of the following queries work identically::
    
  108. 
    
  109.     >>> Person.objects.raw('SELECT id, first_name, last_name, birth_date FROM myapp_person')
    
  110.     ...
    
  111.     >>> Person.objects.raw('SELECT last_name, birth_date, first_name, id FROM myapp_person')
    
  112.     ...
    
  113. 
    
  114. Matching is done by name. This means that you can use SQL's ``AS`` clauses to
    
  115. map fields in the query to model fields. So if you had some other table that
    
  116. had ``Person`` data in it, you could easily map it into ``Person`` instances::
    
  117. 
    
  118.     >>> Person.objects.raw('''SELECT first AS first_name,
    
  119.     ...                              last AS last_name,
    
  120.     ...                              bd AS birth_date,
    
  121.     ...                              pk AS id,
    
  122.     ...                       FROM some_other_table''')
    
  123. 
    
  124. As long as the names match, the model instances will be created correctly.
    
  125. 
    
  126. Alternatively, you can map fields in the query to model fields using the
    
  127. ``translations`` argument to ``raw()``. This is a dictionary mapping names of
    
  128. fields in the query to names of fields on the model. For example, the above
    
  129. query could also be written::
    
  130. 
    
  131.     >>> name_map = {'first': 'first_name', 'last': 'last_name', 'bd': 'birth_date', 'pk': 'id'}
    
  132.     >>> Person.objects.raw('SELECT * FROM some_other_table', translations=name_map)
    
  133. 
    
  134. Index lookups
    
  135. -------------
    
  136. 
    
  137. ``raw()`` supports indexing, so if you need only the first result you can
    
  138. write::
    
  139. 
    
  140.     >>> first_person = Person.objects.raw('SELECT * FROM myapp_person')[0]
    
  141. 
    
  142. However, the indexing and slicing are not performed at the database level. If
    
  143. you have a large number of ``Person`` objects in your database, it is more
    
  144. efficient to limit the query at the SQL level::
    
  145. 
    
  146.     >>> first_person = Person.objects.raw('SELECT * FROM myapp_person LIMIT 1')[0]
    
  147. 
    
  148. Deferring model fields
    
  149. ----------------------
    
  150. 
    
  151. Fields may also be left out::
    
  152. 
    
  153.     >>> people = Person.objects.raw('SELECT id, first_name FROM myapp_person')
    
  154. 
    
  155. The ``Person`` objects returned by this query will be deferred model instances
    
  156. (see :meth:`~django.db.models.query.QuerySet.defer()`). This means that the
    
  157. fields that are omitted from the query will be loaded on demand. For example::
    
  158. 
    
  159.     >>> for p in Person.objects.raw('SELECT id, first_name FROM myapp_person'):
    
  160.     ...     print(p.first_name, # This will be retrieved by the original query
    
  161.     ...           p.last_name) # This will be retrieved on demand
    
  162.     ...
    
  163.     John Smith
    
  164.     Jane Jones
    
  165. 
    
  166. From outward appearances, this looks like the query has retrieved both
    
  167. the first name and last name. However, this example actually issued 3
    
  168. queries. Only the first names were retrieved by the ``raw()`` query -- the
    
  169. last names were both retrieved on demand when they were printed.
    
  170. 
    
  171. There is only one field that you can't leave out - the primary key
    
  172. field. Django uses the primary key to identify model instances, so it
    
  173. must always be included in a raw query. A
    
  174. :class:`~django.core.exceptions.FieldDoesNotExist` exception will be raised if
    
  175. you forget to include the primary key.
    
  176. 
    
  177. Adding annotations
    
  178. ------------------
    
  179. 
    
  180. You can also execute queries containing fields that aren't defined on the
    
  181. model. For example, we could use `PostgreSQL's age() function`__ to get a list
    
  182. of people with their ages calculated by the database::
    
  183. 
    
  184.     >>> people = Person.objects.raw('SELECT *, age(birth_date) AS age FROM myapp_person')
    
  185.     >>> for p in people:
    
  186.     ...     print("%s is %s." % (p.first_name, p.age))
    
  187.     John is 37.
    
  188.     Jane is 42.
    
  189.     ...
    
  190. 
    
  191. You can often avoid using raw SQL to compute annotations by instead using a
    
  192. :ref:`Func() expression <func-expressions>`.
    
  193. 
    
  194. __ https://www.postgresql.org/docs/current/functions-datetime.html
    
  195. 
    
  196. Passing parameters into ``raw()``
    
  197. ---------------------------------
    
  198. 
    
  199. If you need to perform parameterized queries, you can use the ``params``
    
  200. argument to ``raw()``::
    
  201. 
    
  202.     >>> lname = 'Doe'
    
  203.     >>> Person.objects.raw('SELECT * FROM myapp_person WHERE last_name = %s', [lname])
    
  204. 
    
  205. ``params`` is a list or dictionary of parameters. You'll use ``%s``
    
  206. placeholders in the query string for a list, or ``%(key)s``
    
  207. placeholders for a dictionary (where ``key`` is replaced by a
    
  208. dictionary key), regardless of your database engine. Such placeholders will be
    
  209. replaced with parameters from the ``params`` argument.
    
  210. 
    
  211. .. note::
    
  212. 
    
  213.    Dictionary params are not supported with the SQLite backend; with
    
  214.    this backend, you must pass parameters as a list.
    
  215. 
    
  216. .. warning::
    
  217. 
    
  218.     **Do not use string formatting on raw queries or quote placeholders in your
    
  219.     SQL strings!**
    
  220. 
    
  221.     It's tempting to write the above query as::
    
  222. 
    
  223.         >>> query = 'SELECT * FROM myapp_person WHERE last_name = %s' % lname
    
  224.         >>> Person.objects.raw(query)
    
  225. 
    
  226.     You might also think you should write your query like this (with quotes
    
  227.     around ``%s``)::
    
  228. 
    
  229.         >>> query = "SELECT * FROM myapp_person WHERE last_name = '%s'"
    
  230. 
    
  231.     **Don't make either of these mistakes.**
    
  232. 
    
  233.     As discussed in :ref:`sql-injection-protection`, using the ``params``
    
  234.     argument and leaving the placeholders unquoted protects you from `SQL
    
  235.     injection attacks`__, a common exploit where attackers inject arbitrary
    
  236.     SQL into your database. If you use string interpolation or quote the
    
  237.     placeholder, you're at risk for SQL injection.
    
  238. 
    
  239. __ https://en.wikipedia.org/wiki/SQL_injection
    
  240. 
    
  241. .. _executing-custom-sql:
    
  242. 
    
  243. Executing custom SQL directly
    
  244. =============================
    
  245. 
    
  246. Sometimes even :meth:`Manager.raw` isn't quite enough: you might need to
    
  247. perform queries that don't map cleanly to models, or directly execute
    
  248. ``UPDATE``, ``INSERT``, or ``DELETE`` queries.
    
  249. 
    
  250. In these cases, you can always access the database directly, routing around
    
  251. the model layer entirely.
    
  252. 
    
  253. The object ``django.db.connection`` represents the default database
    
  254. connection. To use the database connection, call ``connection.cursor()`` to
    
  255. get a cursor object. Then, call ``cursor.execute(sql, [params])`` to execute
    
  256. the SQL and ``cursor.fetchone()`` or ``cursor.fetchall()`` to return the
    
  257. resulting rows.
    
  258. 
    
  259. For example::
    
  260. 
    
  261.     from django.db import connection
    
  262. 
    
  263.     def my_custom_sql(self):
    
  264.         with connection.cursor() as cursor:
    
  265.             cursor.execute("UPDATE bar SET foo = 1 WHERE baz = %s", [self.baz])
    
  266.             cursor.execute("SELECT foo FROM bar WHERE baz = %s", [self.baz])
    
  267.             row = cursor.fetchone()
    
  268. 
    
  269.         return row
    
  270. 
    
  271. To protect against SQL injection, you must not include quotes around the ``%s``
    
  272. placeholders in the SQL string.
    
  273. 
    
  274. Note that if you want to include literal percent signs in the query, you have to
    
  275. double them in the case you are passing parameters::
    
  276. 
    
  277.      cursor.execute("SELECT foo FROM bar WHERE baz = '30%'")
    
  278.      cursor.execute("SELECT foo FROM bar WHERE baz = '30%%' AND id = %s", [self.id])
    
  279. 
    
  280. If you are using :doc:`more than one database </topics/db/multi-db>`, you can
    
  281. use ``django.db.connections`` to obtain the connection (and cursor) for a
    
  282. specific database. ``django.db.connections`` is a dictionary-like
    
  283. object that allows you to retrieve a specific connection using its
    
  284. alias::
    
  285. 
    
  286.     from django.db import connections
    
  287.     with connections['my_db_alias'].cursor() as cursor:
    
  288.         # Your code here...
    
  289. 
    
  290. By default, the Python DB API will return results without their field names,
    
  291. which means you end up with a ``list`` of values, rather than a ``dict``. At a
    
  292. small performance and memory cost, you can return results as a ``dict`` by
    
  293. using something like this::
    
  294. 
    
  295.     def dictfetchall(cursor):
    
  296.         "Return all rows from a cursor as a dict"
    
  297.         columns = [col[0] for col in cursor.description]
    
  298.         return [
    
  299.             dict(zip(columns, row))
    
  300.             for row in cursor.fetchall()
    
  301.         ]
    
  302. 
    
  303. Another option is to use :func:`collections.namedtuple` from the Python
    
  304. standard library. A ``namedtuple`` is a tuple-like object that has fields
    
  305. accessible by attribute lookup; it's also indexable and iterable. Results are
    
  306. immutable and accessible by field names or indices, which might be useful::
    
  307. 
    
  308.     from collections import namedtuple
    
  309. 
    
  310.     def namedtuplefetchall(cursor):
    
  311.         "Return all rows from a cursor as a namedtuple"
    
  312.         desc = cursor.description
    
  313.         nt_result = namedtuple('Result', [col[0] for col in desc])
    
  314.         return [nt_result(*row) for row in cursor.fetchall()]
    
  315. 
    
  316. Here is an example of the difference between the three::
    
  317. 
    
  318.     >>> cursor.execute("SELECT id, parent_id FROM test LIMIT 2")
    
  319.     >>> cursor.fetchall()
    
  320.     ((54360982, None), (54360880, None))
    
  321. 
    
  322.     >>> cursor.execute("SELECT id, parent_id FROM test LIMIT 2")
    
  323.     >>> dictfetchall(cursor)
    
  324.     [{'parent_id': None, 'id': 54360982}, {'parent_id': None, 'id': 54360880}]
    
  325. 
    
  326.     >>> cursor.execute("SELECT id, parent_id FROM test LIMIT 2")
    
  327.     >>> results = namedtuplefetchall(cursor)
    
  328.     >>> results
    
  329.     [Result(id=54360982, parent_id=None), Result(id=54360880, parent_id=None)]
    
  330.     >>> results[0].id
    
  331.     54360982
    
  332.     >>> results[0][0]
    
  333.     54360982
    
  334. 
    
  335. Connections and cursors
    
  336. -----------------------
    
  337. 
    
  338. ``connection`` and ``cursor`` mostly implement the standard Python DB-API
    
  339. described in :pep:`249` — except when it comes to :doc:`transaction handling
    
  340. </topics/db/transactions>`.
    
  341. 
    
  342. If you're not familiar with the Python DB-API, note that the SQL statement in
    
  343. ``cursor.execute()`` uses placeholders, ``"%s"``, rather than adding
    
  344. parameters directly within the SQL. If you use this technique, the underlying
    
  345. database library will automatically escape your parameters as necessary.
    
  346. 
    
  347. Also note that Django expects the ``"%s"`` placeholder, *not* the ``"?"``
    
  348. placeholder, which is used by the SQLite Python bindings. This is for the sake
    
  349. of consistency and sanity.
    
  350. 
    
  351. Using a cursor as a context manager::
    
  352. 
    
  353.     with connection.cursor() as c:
    
  354.         c.execute(...)
    
  355. 
    
  356. is equivalent to::
    
  357. 
    
  358.     c = connection.cursor()
    
  359.     try:
    
  360.         c.execute(...)
    
  361.     finally:
    
  362.         c.close()
    
  363. 
    
  364. Calling stored procedures
    
  365. ~~~~~~~~~~~~~~~~~~~~~~~~~
    
  366. 
    
  367. .. method:: CursorWrapper.callproc(procname, params=None, kparams=None)
    
  368. 
    
  369.     Calls a database stored procedure with the given name. A sequence
    
  370.     (``params``) or dictionary (``kparams``) of input parameters may be
    
  371.     provided. Most databases don't support ``kparams``. Of Django's built-in
    
  372.     backends, only Oracle supports it.
    
  373. 
    
  374.     For example, given this stored procedure in an Oracle database:
    
  375. 
    
  376.     .. code-block:: sql
    
  377. 
    
  378.         CREATE PROCEDURE "TEST_PROCEDURE"(v_i INTEGER, v_text NVARCHAR2(10)) AS
    
  379.             p_i INTEGER;
    
  380.             p_text NVARCHAR2(10);
    
  381.         BEGIN
    
  382.             p_i := v_i;
    
  383.             p_text := v_text;
    
  384.             ...
    
  385.         END;
    
  386. 
    
  387.     This will call it::
    
  388. 
    
  389.         with connection.cursor() as cursor:
    
  390.             cursor.callproc('test_procedure', [1, 'test'])