indexes.txt 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. =====================
  2. Model index reference
  3. =====================
  4. .. module:: django.db.models.indexes
  5. .. currentmodule:: django.db.models
  6. .. versionadded:: 1.11
  7. Index classes ease creating database indexes. They can be added using the
  8. :attr:`Meta.indexes <django.db.models.Options.indexes>` option. This document
  9. explains the API references of :class:`Index` which includes the `index
  10. options`_.
  11. .. admonition:: Referencing built-in indexes
  12. Indexes are defined in ``django.db.models.indexes``, but for convenience
  13. they're imported into :mod:`django.db.models`. The standard convention is
  14. to use ``from django.db import models`` and refer to the indexes as
  15. ``models.<IndexClass>``.
  16. ``Index`` options
  17. =================
  18. .. class:: Index(fields=[], name=None, db_tablespace=None)
  19. Creates an index (B-Tree) in the database.
  20. ``fields``
  21. ----------
  22. .. attribute:: Index.fields
  23. A list of the name of the fields on which the index is desired.
  24. By default, indexes are created with an ascending order for each column. To
  25. define an index with a descending order for a column, add a hyphen before the
  26. field's name.
  27. For example ``Index(fields=['headline', '-pub_date'])`` would create SQL with
  28. ``(headline, pub_date DESC)``. Index ordering isn't supported on MySQL. In that
  29. case, a descending index is created as a normal index.
  30. .. admonition:: Support for column ordering on SQLite
  31. Column ordering is supported on SQLite 3.3.0+ and only for some database
  32. file formats. Refer to the `SQLite docs
  33. <https://www.sqlite.org/lang_createindex.html>`_ for specifics.
  34. ``name``
  35. --------
  36. .. attribute:: Index.name
  37. The name of the index. If ``name`` isn't provided Django will auto-generate a
  38. name. For compatibility with different databases, index names cannot be longer
  39. than 30 characters and shouldn't start with a number (0-9) or underscore (_).
  40. ``db_tablespace``
  41. -----------------
  42. .. attribute:: Index.db_tablespace
  43. .. versionadded:: 2.0
  44. The name of the :doc:`database tablespace </topics/db/tablespaces>` to use for
  45. this index. For single field indexes, if ``db_tablespace`` isn't provided, the
  46. index is created in the ``db_tablespace`` of the field.
  47. If :attr:`.Field.db_tablespace` isn't specified (or if the index uses multiple
  48. fields), the index is created in tablespace specified in the
  49. :attr:`~django.db.models.Options.db_tablespace` option inside the model's
  50. ``class Meta``. If neither of those tablespaces are set, the index is created
  51. in the same tablespace as the table.
  52. .. seealso::
  53. For a list of PostgreSQL-specific indexes, see
  54. :mod:`django.contrib.postgres.indexes`.