Suppose we start with this situation:
{{{
#!python
from django.db import models
class MyModel(models.Model):
counter = models.IntegerField(defalt=0)
max_value = models.IntegerField(defalt=0)
name = models.CharField(max_length=4)
class Meta:
constraints = (
models.CheckConstraint(
check=models.Q(counter__gte=models.F('max_value')),
name='counter_lower_than_max',
),
models.UniqueConstraint(
fields=('name', 'max_value')
name='uniq_name_and_max',
),
)
}}}
If we add a custom `violation_error_message` and run `makemigrations` it
will output
{{{
$ ./manage.py makemigrations mysample -n update_violation_msgs
Migrations for 'mysample':
mysample/migrations/0002_update_violation_msgs.py
- Remove constraint counter_lower_than_max from model mymodel
- Create constraint counter_lower_than_max on model mymodel
- Remove constraint uniq_name_and_maxfrom model mymodel
- Create constraint uniq_name_and_maxon model mymodel
}}}
This will cause the database tu run useless commands, since nothing on
database side has changed!
This may be a particular problem when applied to `UniqueConstraint`s since
the database needs to destroy and re-create an index.
--
Ticket URL: <https://code.djangoproject.com/ticket/35038>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* stage: Unreviewed => Accepted
Comment:
Thanks for the report 🏆
Aside from running useless commands, dropping & adding constraints on a
production database may be undesirable.
--
Ticket URL: <https://code.djangoproject.com/ticket/35038#comment:1>
* has_patch: 0 => 1
Comment:
PR: https://github.com/django/django/pull/17644
--
Ticket URL: <https://code.djangoproject.com/ticket/35038#comment:2>
* has_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/35038#comment:3>
* owner: nobody => Salvo Polizzi
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/35038#comment:4>
* owner: Salvo Polizzi => (none)
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/35038#comment:5>
* owner: (none) => Nathaniel Conroy
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/35038#comment:6>
* owner: Nathaniel Conroy => (none)
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/35038#comment:7>
* owner: (none) => Adrienne Franke
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/35038#comment:8>