mirror of
https://github.com/django/django.git
synced 2025-07-24 13:44:32 +00:00
Fixed #26808 -- Added Meta.indexes for class-based indexes.
* Added the index name to its deconstruction. * Added indexes to sqlite3.schema._remake_table() so that indexes aren't dropped when _remake_table() is called. Thanks timgraham & MarkusH for review and advice.
This commit is contained in:
parent
d117567c7d
commit
6a8372e6ec
14 changed files with 246 additions and 35 deletions
|
@ -202,22 +202,6 @@ is set, its column name).
|
|||
Creates an index in the database table for the model with ``model_name``.
|
||||
``index`` is an instance of the :class:`~django.db.models.Index` class.
|
||||
|
||||
For example, to add an index on the ``title`` and ``author`` fields of the
|
||||
``Book`` model::
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
operations = [
|
||||
migrations.AddIndex(
|
||||
'Book',
|
||||
models.Index(fields=['title', 'author'], name='my_index_name'),
|
||||
),
|
||||
]
|
||||
|
||||
If you're writing your own migration to add an index, you must assign a
|
||||
``name`` to the ``index`` as done above.
|
||||
|
||||
``RemoveIndex``
|
||||
---------------
|
||||
|
||||
|
|
|
@ -8,8 +8,10 @@ Model index reference
|
|||
|
||||
.. versionadded:: 1.11
|
||||
|
||||
Index classes ease creating database indexes. This document explains the API
|
||||
references of :class:`Index` which includes the `index options`_.
|
||||
Index classes ease creating database indexes. They can be added using the
|
||||
:attr:`Meta.indexes <django.db.models.Options.indexes>` option. This document
|
||||
explains the API references of :class:`Index` which includes the `index
|
||||
options`_.
|
||||
|
||||
.. admonition:: Referencing built-in indexes
|
||||
|
||||
|
@ -40,9 +42,3 @@ A list of the name of the fields on which the index is desired.
|
|||
The name of the index. If ``name`` isn't provided Django will auto-generate a
|
||||
name. For compatibility with different databases, index names cannot be longer
|
||||
than 30 characters and shouldn't start with a number (0-9) or underscore (_).
|
||||
|
||||
.. seealso::
|
||||
|
||||
Use the :class:`~django.db.migrations.operations.AddIndex` and
|
||||
:class:`~django.db.migrations.operations.RemoveIndex` operations to add
|
||||
and remove indexes.
|
||||
|
|
|
@ -390,6 +390,28 @@ Django quotes column and table names behind the scenes.
|
|||
See :meth:`django.db.models.Model.save()` for more about the old and
|
||||
new saving algorithm.
|
||||
|
||||
``indexes``
|
||||
-----------
|
||||
|
||||
.. attribute:: Options.indexes
|
||||
|
||||
.. versionadded:: 1.11
|
||||
|
||||
A list of :doc:`indexes </ref/models/indexes>` that you want to define on
|
||||
the model::
|
||||
|
||||
from django.db import models
|
||||
|
||||
class Customer(models.Model):
|
||||
first_name = models.CharField(max_length=100)
|
||||
last_name = models.CharField(max_length=100)
|
||||
|
||||
class Meta:
|
||||
indexes = [
|
||||
models.Index(fields=['last_name', 'first_name']),
|
||||
models.Index(fields=['first_name'], name='first_name_idx'),
|
||||
]
|
||||
|
||||
``unique_together``
|
||||
-------------------
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue