[Django] #33947: Adding db_index to a field inherited from an Abstract class does not propagate the change to the models subclassing it

8 views
Skip to first unread message

Django

unread,
Aug 22, 2022, 6:40:54 PM8/22/22
to django-...@googlegroups.com
#33947: Adding db_index to a field inherited from an Abstract class does not
propagate the change to the models subclassing it
-------------------------------------+-------------------------------------
Reporter: awiebe | Owner: nobody
Type: | Status: new
Uncategorized |
Component: Database | Version: 3.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I have a bunch of Models of the form
{{{
#!div style="font-size: 80%"
{{{#!python
class AbstractOwnershipModel(models.Model):
owner_user= models.ForeignKey('auth.User')
class Meta:
abstract = True
}}}
}}}

Which I tried to change to
{{{
#!div style="font-size: 80%"
{{{#!python
class AbstractOwnershipModel(models.Model):
owner_user= models.ForeignKey('auth.User',db_index=True)
class Meta:
abstract = True
}}}
}}}

But makemigrations reports that there are no changes even though I would
expect a bunch of indexes to be created.

--
Ticket URL: <https://code.djangoproject.com/ticket/33947>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 22, 2022, 6:44:15 PM8/22/22
to django-...@googlegroups.com
#33947: Adding db_index to a field inherited from an Abstract class does not
propagate the change to the models subclassing it
-------------------------------------+-------------------------------------
Reporter: awiebe | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by awiebe:

Old description:

> I have a bunch of Models of the form
> {{{
> #!div style="font-size: 80%"
> {{{#!python
> class AbstractOwnershipModel(models.Model):
> owner_user= models.ForeignKey('auth.User')
> class Meta:
> abstract = True
> }}}
> }}}
>
> Which I tried to change to
> {{{
> #!div style="font-size: 80%"
> {{{#!python
> class AbstractOwnershipModel(models.Model):
> owner_user= models.ForeignKey('auth.User',db_index=True)
> class Meta:
> abstract = True
> }}}
> }}}
>
> But makemigrations reports that there are no changes even though I would
> expect a bunch of indexes to be created.

New description:

I have a bunch of Models of the form
{{{
#!div style="font-size: 80%"
{{{#!python
class AbstractOwnershipModel(models.Model):
owner_user= models.ForeignKey('auth.User')
class Meta:
abstract = True

class AnOwnedModel(AbstractOwnershipModel):
#...
}}}
}}}

Which I tried to change to
{{{
#!div style="font-size: 80%"
{{{#!python
class AbstractOwnershipModel(models.Model):
owner_user= models.ForeignKey('auth.User',db_index=True)
class Meta:
abstract = True
}}}
}}}

But makemigrations reports that there are no changes even though I would
expect a bunch of indexes to be created.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/33947#comment:1>

Django

unread,
Aug 23, 2022, 1:29:42 AM8/23/22
to django-...@googlegroups.com
#33947: Adding db_index to a field inherited from an Abstract class does not
propagate the change to the models subclassing it
-------------------------------------+-------------------------------------
Reporter: awiebe | Owner: nobody
Type: Uncategorized | Status: closed

Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Alex Morega):

* status: new => closed
* resolution: => invalid


Comment:

>But makemigrations reports that there are no changes even though I would
expect a bunch of indexes to be created.

That's because the index is
[https://docs.djangoproject.com/en/4.1/ref/models/fields/#django.db.models.ForeignKey
already created by default]:

>A database index is automatically created on the `ForeignKey`. You can
disable this by setting `db_index` to `False`. You may want to avoid the
overhead of an index if you are creating a foreign key for consistency
rather than joins, or if you will be creating an alternative index like a
partial or multiple column index.

You can check by connecting to the database using `./manage.py dbshell`
and looking at the database schema.

--
Ticket URL: <https://code.djangoproject.com/ticket/33947#comment:2>

Reply all
Reply to author
Forward
0 new messages