Defining an Index in the Meta of a model ignores the
DEFAULT_INDEX_TABLESPACE setting if multiple fields are used for the
index.
The setting is honoured if only a single field defines the index.
Example case, with a postgresql DB defined in settings, as well as:
{{{
DEFAULT_TABLESPACE = 'data_ts'
DEFAULT_INDEX_TABLESPACE = 'index_ts'
}}}
The below model definition:
{{{
class MyModel(models.Model):
foo = models.CharField(max_length=10)
bar = models.CharField(max_length=10)
class Meta:
indexes = [
models.Index(fields=['foo']),
models.Index(fields=['foo', 'bar']),
]
}}}
generates this output from management command {{{sqlmigrate}}} run on the
produced migration:
{{{
BEGIN;
--
-- Create model MyModel
--
CREATE TABLE "cbcap_mymodel" ("id" bigserial NOT NULL PRIMARY KEY USING
INDEX TABLESPACE "index_ts", "foo" varchar(10) NOT NULL, "bar" varchar(10)
NOT NULL) TABLESPACE "data_ts";
--
-- Create index cbcap_mymod_foo_f01529_idx on field(s) foo of model
mymodel
--
CREATE INDEX "cbcap_mymod_foo_f01529_idx" ON "cbcap_mymodel" ("foo")
TABLESPACE "index_ts";
--
-- Create index cbcap_mymod_foo_44bcd6_idx on field(s) foo, bar of model
mymodel
--
CREATE INDEX "cbcap_mymod_foo_44bcd6_idx" ON "cbcap_mymodel" ("foo",
"bar") TABLESPACE "data_ts";
COMMIT;
}}}
Note the 'data_ts' tablespace is used for the second index
--
Ticket URL: <https://code.djangoproject.com/ticket/33773>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* component: Database layer (models, ORM) => Migrations
* stage: Unreviewed => Accepted
Comment:
Thank you for your report.
There's effectively a discrepancy between
[https://docs.djangoproject.com/en/4.0/topics/db/tablespaces/#declaring-
tablespaces-for-indexes what's documented] and how
[https://github.com/django/django/blob/e96320c91724830034033a9cb8afd9cf8c11e2fd/django/db/backends/base/schema.py#L1305-L1313
it's implemented for indexes including multiple fields or solely
expressions].
Would you be interested in submitting a patch that makes
`_get_index_tablespace_sql` consider `DEFAULT_INDEX_TABLESPACE` before
`model._meta.db_tablespace` if the former is defined?
--
Ticket URL: <https://code.djangoproject.com/ticket/33773#comment:1>
* owner: nobody => Bruce Cutler
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/33773#comment:2>
* has_patch: 0 => 1
* needs_tests: 0 => 1
Comment:
[https://github.com/django/django/pull/15770 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/33773#comment:3>
Comment (by Bruce Cutler):
[[https://github.com/django/django/pull/15770|PR on github]] raised for
this patch.
No tests were created for the patch, as 3 existing tests already fail
prior to the changes (and pass since) when DEFAULT_INDEX_TABLESPACE is
specified in settings:
{{{
======================================================================
FAIL: test_db_tablespace (model_indexes.tests.IndexesTests) [<object
object at 0x7f8bcd80fa00>] (fields=['shortcut', 'isbn'])
...
AssertionError: '"idx_def_tbsp"' not found in 'create index "" on
"model_indexes_book" ("shortcut", "isbn")'
======================================================================
FAIL: test_db_tablespace (model_indexes.tests.IndexesTests) [<object
object at 0x7f8bcd80fa00>] (fields=['title', 'author'])
...
AssertionError: '"idx_def_tbsp"' not found in 'create index "" on
"model_indexes_book" ("title", "author")'
======================================================================
FAIL: test_func_with_tablespace (model_indexes.tests.IndexesTests)
...
AssertionError: '"idx_def_tbsp"' not found in 'CREATE INDEX
"functional_no_tbls" ON "model_indexes_book" ((LOWER("shortcut")) DESC)'
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33773#comment:4>
* needs_tests: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/33773#comment:5>
Comment (by Mariusz Felisiak):
Replying to [comment:4 Bruce Cutler]:
> [[https://github.com/django/django/pull/15770|PR on github]] raised for
this patch.
>
> No tests were created for the patch, as 3 existing tests already fail
prior to the changes (and pass since) when DEFAULT_INDEX_TABLESPACE is
specified in settings:
> {{{
> ======================================================================
> FAIL: test_db_tablespace (model_indexes.tests.IndexesTests) [<object
object at 0x7f8bcd80fa00>] (fields=['shortcut', 'isbn'])
> ...
> AssertionError: '"idx_def_tbsp"' not found in 'create index "" on
"model_indexes_book" ("shortcut", "isbn")'
>
> ======================================================================
> FAIL: test_db_tablespace (model_indexes.tests.IndexesTests) [<object
object at 0x7f8bcd80fa00>] (fields=['title', 'author'])
> ...
> AssertionError: '"idx_def_tbsp"' not found in 'create index "" on
"model_indexes_book" ("title", "author")'
>
> ======================================================================
> FAIL: test_func_with_tablespace (model_indexes.tests.IndexesTests)
> ...
> AssertionError: '"idx_def_tbsp"' not found in 'CREATE INDEX
"functional_no_tbls" ON "model_indexes_book" ((LOWER("shortcut")) DESC)'
> }}}
Unfortunately it's not covered by CI because we set the same tablespace to
the `DEFAULT_TABLESPACE` and `DEFAULT_INDEX_TABLESPACE`. Overriding
`DEFAULT_TABLESPACE` to `None` should make it covered.
--
Ticket URL: <https://code.djangoproject.com/ticket/33773#comment:6>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/33773#comment:7>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"de1c8320cedee5d4f91b153a7fe82eb63876470f" de1c8320]:
{{{
#!CommitTicketReference repository=""
revision="de1c8320cedee5d4f91b153a7fe82eb63876470f"
Fixed #33773 -- Made Index with multiple fields respect
DEFAULT_INDEX_TABLESPACE.
Thanks to Simon Charette for locating where issue lay.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33773#comment:8>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"a7f398ae597536d0cee67cb525f165d87ca2fe49" a7f398ae]:
{{{
#!CommitTicketReference repository=""
revision="a7f398ae597536d0cee67cb525f165d87ca2fe49"
[4.1.x] Fixed #33773 -- Made Index with multiple fields respect
DEFAULT_INDEX_TABLESPACE.
Thanks to Simon Charette for locating where issue lay.
Backport of de1c8320cedee5d4f91b153a7fe82eb63876470f from main
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33773#comment:9>