databases.txt 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. =========
  2. Databases
  3. =========
  4. Django attempts to support as many features as possible on all database
  5. backends. However, not all database backends are alike, and we've had to make
  6. design decisions on which features to support and which assumptions we can make
  7. safely.
  8. This file describes some of the features that might be relevant to Django
  9. usage. Of course, it is not intended as a replacement for server-specific
  10. documentation or reference manuals.
  11. General notes
  12. =============
  13. .. _persistent-database-connections:
  14. Persistent connections
  15. ----------------------
  16. .. versionadded:: 1.6
  17. Persistent connections avoid the overhead of re-establishing a connection to
  18. the database in each request. They're controlled by the
  19. :setting:`CONN_MAX_AGE` parameter which defines the maximum lifetime of a
  20. connection. It can be set independently for each database.
  21. The default value is ``0``, preserving the historical behavior of closing the
  22. database connection at the end of each request. To enable persistent
  23. connections, set :setting:`CONN_MAX_AGE` to a positive number of seconds. For
  24. unlimited persistent connections, set it to ``None``.
  25. Connection management
  26. ~~~~~~~~~~~~~~~~~~~~~
  27. Django opens a connection to the database when it first makes a database
  28. query. It keeps this connection open and reuses it in subsequent requests.
  29. Django closes the connection once it exceeds the maximum age defined by
  30. :setting:`CONN_MAX_AGE` or when it isn't usable any longer.
  31. In detail, Django automatically opens a connection to the database whenever it
  32. needs one and doesn't have one already — either because this is the first
  33. connection, or because the previous connection was closed.
  34. At the beginning of each request, Django closes the connection if it has
  35. reached its maximum age. If your database terminates idle connections after
  36. some time, you should set :setting:`CONN_MAX_AGE` to a lower value, so that
  37. Django doesn't attempt to use a connection that has been terminated by the
  38. database server. (This problem may only affect very low traffic sites.)
  39. At the end of each request, Django closes the connection if it has reached its
  40. maximum age or if it is in an unrecoverable error state. If any database
  41. errors have occurred while processing the requests, Django checks whether the
  42. connection still works, and closes it if it doesn't. Thus, database errors
  43. affect at most one request; if the connection becomes unusable, the next
  44. request gets a fresh connection.
  45. Caveats
  46. ~~~~~~~
  47. Since each thread maintains its own connection, your database must support at
  48. least as many simultaneous connections as you have worker threads.
  49. Sometimes a database won't be accessed by the majority of your views, for
  50. example because it's the database of an external system, or thanks to caching.
  51. In such cases, you should set :setting:`CONN_MAX_AGE` to a low value or even
  52. ``0``, because it doesn't make sense to maintain a connection that's unlikely
  53. to be reused. This will help keep the number of simultaneous connections to
  54. this database small.
  55. The development server creates a new thread for each request it handles,
  56. negating the effect of persistent connections. Don't enable them during
  57. development.
  58. When Django establishes a connection to the database, it sets up appropriate
  59. parameters, depending on the backend being used. If you enable persistent
  60. connections, this setup is no longer repeated every request. If you modify
  61. parameters such as the connection's isolation level or time zone, you should
  62. either restore Django's defaults at the end of each request, force an
  63. appropriate value at the beginning of each request, or disable persistent
  64. connections.
  65. .. _postgresql-notes:
  66. PostgreSQL notes
  67. ================
  68. Django supports PostgreSQL 8.4 and higher.
  69. PostgreSQL connection settings
  70. -------------------------------
  71. See :setting:`HOST` for details.
  72. Optimizing PostgreSQL's configuration
  73. -------------------------------------
  74. Django needs the following parameters for its database connections:
  75. - ``client_encoding``: ``'UTF8'``,
  76. - ``default_transaction_isolation``: ``'read committed'`` by default,
  77. or the value set in the connection options (see below),
  78. - ``timezone``: ``'UTC'`` when :setting:`USE_TZ` is ``True``, value of
  79. :setting:`TIME_ZONE` otherwise.
  80. If these parameters already have the correct values, Django won't set them for
  81. every new connection, which improves performance slightly. You can configure
  82. them directly in :file:`postgresql.conf` or more conveniently per database
  83. user with `ALTER ROLE`_.
  84. Django will work just fine without this optimization, but each new connection
  85. will do some additional queries to set these parameters.
  86. .. _ALTER ROLE: http://www.postgresql.org/docs/current/interactive/sql-alterrole.html
  87. .. _postgresql-autocommit-mode:
  88. Autocommit mode
  89. ---------------
  90. .. versionchanged:: 1.6
  91. In previous versions of Django, database-level autocommit could be enabled by
  92. setting the ``autocommit`` key in the :setting:`OPTIONS` part of your database
  93. configuration in :setting:`DATABASES`::
  94. DATABASES = {
  95. # ...
  96. 'OPTIONS': {
  97. 'autocommit': True,
  98. },
  99. }
  100. Since Django 1.6, autocommit is turned on by default. This configuration is
  101. ignored and can be safely removed.
  102. .. _database-isolation-level:
  103. Isolation level
  104. ---------------
  105. .. versionadded:: 1.6
  106. Like PostgreSQL itself, Django defaults to the ``READ COMMITTED`` `isolation
  107. level`_. If you need a higher isolation level such as ``REPEATABLE READ`` or
  108. ``SERIALIZABLE``, set it in the :setting:`OPTIONS` part of your database
  109. configuration in :setting:`DATABASES`::
  110. import psycopg2.extensions
  111. DATABASES = {
  112. # ...
  113. 'OPTIONS': {
  114. 'isolation_level': psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE,
  115. },
  116. }
  117. .. note::
  118. Under higher isolation levels, your application should be prepared to
  119. handle exceptions raised on serialization failures. This option is
  120. designed for advanced uses.
  121. .. _isolation level: http://www.postgresql.org/docs/current/static/transaction-iso.html
  122. Indexes for ``varchar`` and ``text`` columns
  123. --------------------------------------------
  124. When specifying ``db_index=True`` on your model fields, Django typically
  125. outputs a single ``CREATE INDEX`` statement. However, if the database type
  126. for the field is either ``varchar`` or ``text`` (e.g., used by ``CharField``,
  127. ``FileField``, and ``TextField``), then Django will create
  128. an additional index that uses an appropriate `PostgreSQL operator class`_
  129. for the column. The extra index is necessary to correctly perform
  130. lookups that use the ``LIKE`` operator in their SQL, as is done with the
  131. ``contains`` and ``startswith`` lookup types.
  132. .. _PostgreSQL operator class: http://www.postgresql.org/docs/current/static/indexes-opclass.html
  133. .. _mysql-notes:
  134. MySQL notes
  135. ===========
  136. Version support
  137. ---------------
  138. Django supports MySQL 5.0.3 and higher.
  139. `MySQL 5.0`_ adds the ``information_schema`` database, which contains detailed
  140. data on all database schema. Django's ``inspectdb`` feature uses it.
  141. Django expects the database to support Unicode (UTF-8 encoding) and delegates to
  142. it the task of enforcing transactions and referential integrity. It is important
  143. to be aware of the fact that the two latter ones aren't actually enforced by
  144. MySQL when using the MyISAM storage engine, see the next section.
  145. .. _MySQL: http://www.mysql.com/
  146. .. _MySQL 5.0: http://dev.mysql.com/doc/refman/5.0/en/index.html
  147. .. _mysql-storage-engines:
  148. Storage engines
  149. ---------------
  150. MySQL has several `storage engines`_. You can change the default storage engine
  151. in the server configuration.
  152. Until MySQL 5.5.4, the default engine was MyISAM_ [#]_. The main drawbacks of
  153. MyISAM are that it doesn't support transactions or enforce foreign-key
  154. constraints. On the plus side, it's currently the only engine that supports
  155. full-text indexing and searching.
  156. Since MySQL 5.5.5, the default storage engine is InnoDB_. This engine is fully
  157. transactional and supports foreign key references. It's probably the best
  158. choice at this point.
  159. If you upgrade an existing project to MySQL 5.5.5 and subsequently add some
  160. tables, ensure that your tables are using the same storage engine (i.e. MyISAM
  161. vs. InnoDB). Specifically, if tables that have a ``ForeignKey`` between them
  162. use different storage engines, you may see an error like the following when
  163. running ``syncdb``::
  164. _mysql_exceptions.OperationalError: (
  165. 1005, "Can't create table '\\db_name\\.#sql-4a8_ab' (errno: 150)"
  166. )
  167. .. _storage engines: http://dev.mysql.com/doc/refman/5.5/en/storage-engines.html
  168. .. _MyISAM: http://dev.mysql.com/doc/refman/5.5/en/myisam-storage-engine.html
  169. .. _InnoDB: http://dev.mysql.com/doc/refman/5.5/en/innodb-storage-engine.html
  170. .. [#] Unless this was changed by the packager of your MySQL package. We've
  171. had reports that the Windows Community Server installer sets up InnoDB as
  172. the default storage engine, for example.
  173. MySQLdb
  174. -------
  175. `MySQLdb`_ is the Python interface to MySQL. Version 1.2.1p2 or later is
  176. required for full MySQL support in Django.
  177. .. note::
  178. If you see ``ImportError: cannot import name ImmutableSet`` when trying to
  179. use Django, your MySQLdb installation may contain an outdated ``sets.py``
  180. file that conflicts with the built-in module of the same name from Python
  181. 2.4 and later. To fix this, verify that you have installed MySQLdb version
  182. 1.2.1p2 or newer, then delete the ``sets.py`` file in the MySQLdb
  183. directory that was left by an earlier version.
  184. .. note::
  185. There are known issues with the way MySQLdb converts date strings into
  186. datetime objects. Specifically, date strings with value 0000-00-00 are valid for
  187. MySQL but will be converted into None by MySQLdb.
  188. This means you should be careful while using loaddata/dumpdata with rows
  189. that may have 0000-00-00 values, as they will be converted to None.
  190. .. _MySQLdb: http://sourceforge.net/projects/mysql-python
  191. Python 3
  192. --------
  193. At the time of writing, the latest release of MySQLdb (1.2.4) doesn't support
  194. Python 3. In order to use MySQL under Python 3, you'll have to install an
  195. unofficial fork, such as `MySQL-for-Python-3`_.
  196. This port is still in alpha. In particular, it doesn't support binary data,
  197. making it impossible to use :class:`django.db.models.BinaryField`.
  198. .. _MySQL-for-Python-3: https://github.com/clelland/MySQL-for-Python-3
  199. Creating your database
  200. ----------------------
  201. You can `create your database`_ using the command-line tools and this SQL::
  202. CREATE DATABASE <dbname> CHARACTER SET utf8;
  203. This ensures all tables and columns will use UTF-8 by default.
  204. .. _create your database: http://dev.mysql.com/doc/refman/5.0/en/create-database.html
  205. .. _mysql-collation:
  206. Collation settings
  207. ~~~~~~~~~~~~~~~~~~
  208. The collation setting for a column controls the order in which data is sorted
  209. as well as what strings compare as equal. It can be set on a database-wide
  210. level and also per-table and per-column. This is `documented thoroughly`_ in
  211. the MySQL documentation. In all cases, you set the collation by directly
  212. manipulating the database tables; Django doesn't provide a way to set this on
  213. the model definition.
  214. .. _documented thoroughly: http://dev.mysql.com/doc/refman/5.0/en/charset.html
  215. By default, with a UTF-8 database, MySQL will use the
  216. ``utf8_general_ci_swedish`` collation. This results in all string equality
  217. comparisons being done in a *case-insensitive* manner. That is, ``"Fred"`` and
  218. ``"freD"`` are considered equal at the database level. If you have a unique
  219. constraint on a field, it would be illegal to try to insert both ``"aa"`` and
  220. ``"AA"`` into the same column, since they compare as equal (and, hence,
  221. non-unique) with the default collation.
  222. In many cases, this default will not be a problem. However, if you really want
  223. case-sensitive comparisons on a particular column or table, you would change
  224. the column or table to use the ``utf8_bin`` collation. The main thing to be
  225. aware of in this case is that if you are using MySQLdb 1.2.2, the database
  226. backend in Django will then return bytestrings (instead of unicode strings) for
  227. any character fields it receive from the database. This is a strong variation
  228. from Django's normal practice of *always* returning unicode strings. It is up
  229. to you, the developer, to handle the fact that you will receive bytestrings if
  230. you configure your table(s) to use ``utf8_bin`` collation. Django itself should
  231. mostly work smoothly with such columns (except for the ``contrib.sessions``
  232. ``Session`` and ``contrib.admin`` ``LogEntry`` tables described below), but
  233. your code must be prepared to call ``django.utils.encoding.smart_text()`` at
  234. times if it really wants to work with consistent data -- Django will not do
  235. this for you (the database backend layer and the model population layer are
  236. separated internally so the database layer doesn't know it needs to make this
  237. conversion in this one particular case).
  238. If you're using MySQLdb 1.2.1p2, Django's standard
  239. :class:`~django.db.models.CharField` class will return unicode strings even
  240. with ``utf8_bin`` collation. However, :class:`~django.db.models.TextField`
  241. fields will be returned as an ``array.array`` instance (from Python's standard
  242. ``array`` module). There isn't a lot Django can do about that, since, again,
  243. the information needed to make the necessary conversions isn't available when
  244. the data is read in from the database. This problem was `fixed in MySQLdb
  245. 1.2.2`_, so if you want to use :class:`~django.db.models.TextField` with
  246. ``utf8_bin`` collation, upgrading to version 1.2.2 and then dealing with the
  247. bytestrings (which shouldn't be too difficult) as described above is the
  248. recommended solution.
  249. Should you decide to use ``utf8_bin`` collation for some of your tables with
  250. MySQLdb 1.2.1p2 or 1.2.2, you should still use ``utf8_collation_ci_swedish``
  251. (the default) collation for the ``django.contrib.sessions.models.Session``
  252. table (usually called ``django_session``) and the
  253. ``django.contrib.admin.models.LogEntry`` table (usually called
  254. ``django_admin_log``). Those are the two standard tables that use
  255. :class:`~django.db.models.TextField` internally.
  256. .. _fixed in MySQLdb 1.2.2: http://sourceforge.net/tracker/index.php?func=detail&aid=1495765&group_id=22307&atid=374932
  257. Connecting to the database
  258. --------------------------
  259. Refer to the :doc:`settings documentation </ref/settings>`.
  260. Connection settings are used in this order:
  261. 1. :setting:`OPTIONS`.
  262. 2. :setting:`NAME`, :setting:`USER`, :setting:`PASSWORD`,
  263. :setting:`HOST`, :setting:`PORT`
  264. 3. MySQL option files.
  265. In other words, if you set the name of the database in :setting:`OPTIONS`,
  266. this will take precedence over :setting:`NAME`, which would override
  267. anything in a `MySQL option file`_.
  268. Here's a sample configuration which uses a MySQL option file::
  269. # settings.py
  270. DATABASES = {
  271. 'default': {
  272. 'ENGINE': 'django.db.backends.mysql',
  273. 'OPTIONS': {
  274. 'read_default_file': '/path/to/my.cnf',
  275. },
  276. }
  277. }
  278. # my.cnf
  279. [client]
  280. database = NAME
  281. user = USER
  282. password = PASSWORD
  283. default-character-set = utf8
  284. Several other MySQLdb connection options may be useful, such as ``ssl``,
  285. ``init_command``, and ``sql_mode``. Consult the `MySQLdb documentation`_ for
  286. more details.
  287. .. _MySQL option file: http://dev.mysql.com/doc/refman/5.0/en/option-files.html
  288. .. _MySQLdb documentation: http://mysql-python.sourceforge.net/
  289. Creating your tables
  290. --------------------
  291. When Django generates the schema, it doesn't specify a storage engine, so
  292. tables will be created with whatever default storage engine your database
  293. server is configured for. The easiest solution is to set your database server's
  294. default storage engine to the desired engine.
  295. If you're using a hosting service and can't change your server's default
  296. storage engine, you have a couple of options.
  297. * After the tables are created, execute an ``ALTER TABLE`` statement to
  298. convert a table to a new storage engine (such as InnoDB)::
  299. ALTER TABLE <tablename> ENGINE=INNODB;
  300. This can be tedious if you have a lot of tables.
  301. * Another option is to use the ``init_command`` option for MySQLdb prior to
  302. creating your tables::
  303. 'OPTIONS': {
  304. 'init_command': 'SET storage_engine=INNODB',
  305. }
  306. This sets the default storage engine upon connecting to the database.
  307. After your tables have been created, you should remove this option as it
  308. adds a query that is only needed during table creation to each database
  309. connection.
  310. * Another method for changing the storage engine is described in
  311. AlterModelOnSyncDB_.
  312. .. _AlterModelOnSyncDB: https://code.djangoproject.com/wiki/AlterModelOnSyncDB
  313. Table names
  314. -----------
  315. There are `known issues`_ in even the latest versions of MySQL that can cause the
  316. case of a table name to be altered when certain SQL statements are executed
  317. under certain conditions. It is recommended that you use lowercase table
  318. names, if possible, to avoid any problems that might arise from this behavior.
  319. Django uses lowercase table names when it auto-generates table names from
  320. models, so this is mainly a consideration if you are overriding the table name
  321. via the :class:`~django.db.models.Options.db_table` parameter.
  322. .. _known issues: http://bugs.mysql.com/bug.php?id=48875
  323. Savepoints
  324. ----------
  325. Both the Django ORM and MySQL (when using the InnoDB :ref:`storage engine
  326. <mysql-storage-engines>`) support database :ref:`savepoints
  327. <topics-db-transactions-savepoints>`.
  328. If you use the MyISAM storage engine please be aware of the fact that you will
  329. receive database-generated errors if you try to use the :ref:`savepoint-related
  330. methods of the transactions API <topics-db-transactions-savepoints>`. The reason
  331. for this is that detecting the storage engine of a MySQL database/table is an
  332. expensive operation so it was decided it isn't worth to dynamically convert
  333. these methods in no-op's based in the results of such detection.
  334. Notes on specific fields
  335. ------------------------
  336. Character fields
  337. ~~~~~~~~~~~~~~~~
  338. Any fields that are stored with ``VARCHAR`` column types have their
  339. ``max_length`` restricted to 255 characters if you are using ``unique=True``
  340. for the field. This affects :class:`~django.db.models.CharField`,
  341. :class:`~django.db.models.SlugField` and
  342. :class:`~django.db.models.CommaSeparatedIntegerField`.
  343. DateTime fields
  344. ~~~~~~~~~~~~~~~
  345. MySQL does not have a timezone-aware column type. If an attempt is made to
  346. store a timezone-aware ``time`` or ``datetime`` to a
  347. :class:`~django.db.models.TimeField` or :class:`~django.db.models.DateTimeField`
  348. respectively, a ``ValueError`` is raised rather than truncating data.
  349. MySQL does not store fractions of seconds. Fractions of seconds are truncated
  350. to zero when the time is stored.
  351. Row locking with ``QuerySet.select_for_update()``
  352. -------------------------------------------------
  353. MySQL does not support the ``NOWAIT`` option to the ``SELECT ... FOR UPDATE``
  354. statement. If ``select_for_update()`` is used with ``nowait=True`` then a
  355. ``DatabaseError`` will be raised.
  356. .. _sqlite-notes:
  357. SQLite notes
  358. ============
  359. SQLite_ provides an excellent development alternative for applications that
  360. are predominantly read-only or require a smaller installation footprint. As
  361. with all database servers, though, there are some differences that are
  362. specific to SQLite that you should be aware of.
  363. .. _SQLite: http://www.sqlite.org/
  364. .. _sqlite-string-matching:
  365. Substring matching and case sensitivity
  366. -----------------------------------------
  367. For all SQLite versions, there is some slightly counter-intuitive behavior when
  368. attempting to match some types of strings. These are triggered when using the
  369. :lookup:`iexact` or :lookup:`contains` filters in Querysets. The behavior
  370. splits into two cases:
  371. 1. For substring matching, all matches are done case-insensitively. That is a
  372. filter such as ``filter(name__contains="aa")`` will match a name of ``"Aabb"``.
  373. 2. For strings containing characters outside the ASCII range, all exact string
  374. matches are performed case-sensitively, even when the case-insensitive options
  375. are passed into the query. So the :lookup:`iexact` filter will behave exactly
  376. the same as the :lookup:`exact` filter in these cases.
  377. Some possible workarounds for this are `documented at sqlite.org`_, but they
  378. aren't utilised by the default SQLite backend in Django, as incorporating them
  379. would be fairly difficult to do robustly. Thus, Django exposes the default
  380. SQLite behavior and you should be aware of this when doing case-insensitive or
  381. substring filtering.
  382. .. _documented at sqlite.org: http://www.sqlite.org/faq.html#q18
  383. SQLite 3.3.6 or newer strongly recommended
  384. ------------------------------------------
  385. Versions of SQLite 3.3.5 and older contains the following bugs:
  386. * A bug when `handling`_ ``ORDER BY`` parameters. This can cause problems when
  387. you use the ``select`` parameter for the ``extra()`` QuerySet method. The bug
  388. can be identified by the error message ``OperationalError: ORDER BY terms
  389. must not be non-integer constants``.
  390. * A bug when handling `aggregation`_ together with DateFields and
  391. DecimalFields.
  392. .. _handling: http://www.sqlite.org/cvstrac/tktview?tn=1768
  393. .. _aggregation: https://code.djangoproject.com/ticket/10031
  394. SQLite 3.3.6 was released in April 2006, so most current binary distributions
  395. for different platforms include newer version of SQLite usable from Python
  396. through either the ``pysqlite2`` or the ``sqlite3`` modules.
  397. Version 3.5.9
  398. -------------
  399. The Ubuntu "Intrepid Ibex" (8.10) SQLite 3.5.9-3 package contains a bug that
  400. causes problems with the evaluation of query expressions. If you are using
  401. Ubuntu "Intrepid Ibex", you will need to update the package to version
  402. 3.5.9-3ubuntu1 or newer (recommended) or find an alternate source for SQLite
  403. packages, or install SQLite from source.
  404. At one time, Debian Lenny shipped with the same malfunctioning SQLite 3.5.9-3
  405. package. However the Debian project has subsequently issued updated versions
  406. of the SQLite package that correct these bugs. If you find you are getting
  407. unexpected results under Debian, ensure you have updated your SQLite package
  408. to 3.5.9-5 or later.
  409. The problem does not appear to exist with other versions of SQLite packaged
  410. with other operating systems.
  411. Version 3.6.2
  412. --------------
  413. SQLite version 3.6.2 (released August 30, 2008) introduced a bug into ``SELECT
  414. DISTINCT`` handling that is triggered by, amongst other things, Django's
  415. ``DateQuerySet`` (returned by the ``dates()`` method on a queryset).
  416. You should avoid using this version of SQLite with Django. Either upgrade to
  417. 3.6.3 (released September 22, 2008) or later, or downgrade to an earlier
  418. version of SQLite.
  419. .. _using-newer-versions-of-pysqlite:
  420. Using newer versions of the SQLite DB-API 2.0 driver
  421. ----------------------------------------------------
  422. For versions of Python 2.5 or newer that include ``sqlite3`` in the standard
  423. library Django will now use a ``pysqlite2`` interface in preference to
  424. ``sqlite3`` if it finds one is available.
  425. This provides the ability to upgrade both the DB-API 2.0 interface or SQLite 3
  426. itself to versions newer than the ones included with your particular Python
  427. binary distribution, if needed.
  428. "Database is locked" errors
  429. ---------------------------
  430. SQLite is meant to be a lightweight database, and thus can't support a high
  431. level of concurrency. ``OperationalError: database is locked`` errors indicate
  432. that your application is experiencing more concurrency than ``sqlite`` can
  433. handle in default configuration. This error means that one thread or process has
  434. an exclusive lock on the database connection and another thread timed out
  435. waiting for the lock the be released.
  436. Python's SQLite wrapper has
  437. a default timeout value that determines how long the second thread is allowed to
  438. wait on the lock before it times out and raises the ``OperationalError: database
  439. is locked`` error.
  440. If you're getting this error, you can solve it by:
  441. * Switching to another database backend. At a certain point SQLite becomes
  442. too "lite" for real-world applications, and these sorts of concurrency
  443. errors indicate you've reached that point.
  444. * Rewriting your code to reduce concurrency and ensure that database
  445. transactions are short-lived.
  446. * Increase the default timeout value by setting the ``timeout`` database
  447. option option::
  448. 'OPTIONS': {
  449. # ...
  450. 'timeout': 20,
  451. # ...
  452. }
  453. This will simply make SQLite wait a bit longer before throwing "database
  454. is locked" errors; it won't really do anything to solve them.
  455. ``QuerySet.select_for_update()`` not supported
  456. ----------------------------------------------
  457. SQLite does not support the ``SELECT ... FOR UPDATE`` syntax. Calling it will
  458. have no effect.
  459. "pyformat" parameter style in raw queries not supported
  460. -------------------------------------------------------
  461. For most backends, raw queries (``Manager.raw()`` or ``cursor.execute()``)
  462. can use the "pyformat" parameter style, where placeholders in the query
  463. are given as ``'%(name)s'`` and the parameters are passed as a dictionary
  464. rather than a list. SQLite does not support this.
  465. .. _sqlite-connection-queries:
  466. Parameters not quoted in ``connection.queries``
  467. -----------------------------------------------
  468. ``sqlite3`` does not provide a way to retrieve the SQL after quoting and
  469. substituting the parameters. Instead, the SQL in ``connection.queries`` is
  470. rebuilt with a simple string interpolation. It may be incorrect. Make sure
  471. you add quotes where necessary before copying a query into a SQLite shell.
  472. .. _oracle-notes:
  473. Oracle notes
  474. ============
  475. Django supports `Oracle Database Server`_ versions 9i and
  476. higher. Oracle version 10g or later is required to use Django's
  477. ``regex`` and ``iregex`` query operators. You will also need at least
  478. version 4.3.1 of the `cx_Oracle`_ Python driver.
  479. Note that due to a Unicode-corruption bug in ``cx_Oracle`` 5.0, that
  480. version of the driver should **not** be used with Django;
  481. ``cx_Oracle`` 5.0.1 resolved this issue, so if you'd like to use a
  482. more recent ``cx_Oracle``, use version 5.0.1.
  483. ``cx_Oracle`` 5.0.1 or greater can optionally be compiled with the
  484. ``WITH_UNICODE`` environment variable. This is recommended but not
  485. required.
  486. .. _`Oracle Database Server`: http://www.oracle.com/
  487. .. _`cx_Oracle`: http://cx-oracle.sourceforge.net/
  488. In order for the ``python manage.py syncdb`` command to work, your Oracle
  489. database user must have privileges to run the following commands:
  490. * CREATE TABLE
  491. * CREATE SEQUENCE
  492. * CREATE PROCEDURE
  493. * CREATE TRIGGER
  494. To run Django's test suite, the user needs these *additional* privileges:
  495. * CREATE USER
  496. * DROP USER
  497. * CREATE TABLESPACE
  498. * DROP TABLESPACE
  499. * CONNECT WITH ADMIN OPTION
  500. * RESOURCE WITH ADMIN OPTION
  501. Connecting to the database
  502. --------------------------
  503. Your Django settings.py file should look something like this for Oracle::
  504. DATABASES = {
  505. 'default': {
  506. 'ENGINE': 'django.db.backends.oracle',
  507. 'NAME': 'xe',
  508. 'USER': 'a_user',
  509. 'PASSWORD': 'a_password',
  510. 'HOST': '',
  511. 'PORT': '',
  512. }
  513. }
  514. If you don't use a ``tnsnames.ora`` file or a similar naming method that
  515. recognizes the SID ("xe" in this example), then fill in both
  516. :setting:`HOST` and :setting:`PORT` like so::
  517. DATABASES = {
  518. 'default': {
  519. 'ENGINE': 'django.db.backends.oracle',
  520. 'NAME': 'xe',
  521. 'USER': 'a_user',
  522. 'PASSWORD': 'a_password',
  523. 'HOST': 'dbprod01ned.mycompany.com',
  524. 'PORT': '1540',
  525. }
  526. }
  527. You should supply both :setting:`HOST` and :setting:`PORT`, or leave both
  528. as empty strings.
  529. Threaded option
  530. ----------------
  531. If you plan to run Django in a multithreaded environment (e.g. Apache using the
  532. the default MPM module on any modern operating system), then you **must** set
  533. the ``threaded`` option of your Oracle database configuration to True::
  534. 'OPTIONS': {
  535. 'threaded': True,
  536. },
  537. Failure to do this may result in crashes and other odd behavior.
  538. INSERT ... RETURNING INTO
  539. -------------------------
  540. By default, the Oracle backend uses a ``RETURNING INTO`` clause to efficiently
  541. retrieve the value of an ``AutoField`` when inserting new rows. This behavior
  542. may result in a ``DatabaseError`` in certain unusual setups, such as when
  543. inserting into a remote table, or into a view with an ``INSTEAD OF`` trigger.
  544. The ``RETURNING INTO`` clause can be disabled by setting the
  545. ``use_returning_into`` option of the database configuration to False::
  546. 'OPTIONS': {
  547. 'use_returning_into': False,
  548. },
  549. In this case, the Oracle backend will use a separate ``SELECT`` query to
  550. retrieve AutoField values.
  551. Naming issues
  552. -------------
  553. Oracle imposes a name length limit of 30 characters. To accommodate this, the
  554. backend truncates database identifiers to fit, replacing the final four
  555. characters of the truncated name with a repeatable MD5 hash value.
  556. When running syncdb, an ``ORA-06552`` error may be encountered if
  557. certain Oracle keywords are used as the name of a model field or the
  558. value of a ``db_column`` option. Django quotes all identifiers used
  559. in queries to prevent most such problems, but this error can still
  560. occur when an Oracle datatype is used as a column name. In
  561. particular, take care to avoid using the names ``date``,
  562. ``timestamp``, ``number`` or ``float`` as a field name.
  563. NULL and empty strings
  564. ----------------------
  565. Django generally prefers to use the empty string ('') rather than
  566. NULL, but Oracle treats both identically. To get around this, the
  567. Oracle backend ignores an explicit ``null`` option on fields that
  568. have the empty string as a possible value and generates DDL as if
  569. ``null=True``. When fetching from the database, it is assumed that
  570. a ``NULL`` value in one of these fields really means the empty
  571. string, and the data is silently converted to reflect this assumption.
  572. ``TextField`` limitations
  573. -------------------------
  574. The Oracle backend stores ``TextFields`` as ``NCLOB`` columns. Oracle imposes
  575. some limitations on the usage of such LOB columns in general:
  576. * LOB columns may not be used as primary keys.
  577. * LOB columns may not be used in indexes.
  578. * LOB columns may not be used in a ``SELECT DISTINCT`` list. This means that
  579. attempting to use the ``QuerySet.distinct`` method on a model that
  580. includes ``TextField`` columns will result in an error when run against
  581. Oracle. As a workaround, use the ``QuerySet.defer`` method in conjunction
  582. with ``distinct()`` to prevent ``TextField`` columns from being included in
  583. the ``SELECT DISTINCT`` list.
  584. .. _third-party-notes:
  585. Using a 3rd-party database backend
  586. ==================================
  587. In addition to the officially supported databases, there are backends provided
  588. by 3rd parties that allow you to use other databases with Django:
  589. * `Sybase SQL Anywhere`_
  590. * `IBM DB2`_
  591. * `Microsoft SQL Server 2005`_
  592. * Firebird_
  593. * ODBC_
  594. * ADSDB_
  595. The Django versions and ORM features supported by these unofficial backends
  596. vary considerably. Queries regarding the specific capabilities of these
  597. unofficial backends, along with any support queries, should be directed to
  598. the support channels provided by each 3rd party project.
  599. .. _Sybase SQL Anywhere: http://code.google.com/p/sqlany-django/
  600. .. _IBM DB2: http://code.google.com/p/ibm-db/
  601. .. _Microsoft SQL Server 2005: http://code.google.com/p/django-mssql/
  602. .. _Firebird: http://code.google.com/p/django-firebird/
  603. .. _ODBC: https://github.com/aurorasoftware/django-pyodbc/
  604. .. _ADSDB: http://code.google.com/p/adsdb-django/