class Meta:
constraints = [
models.CheckConstraint(check=models.Q(f1__gte=0),
name='TestCheck_f1_check'),
]
# initial migration:
migrations.CreateModel(
name='TestCheck',
fields=[
('id', models.AutoField(auto_created=True,
primary_key=True, serialize=False, verbose_name='ID')),
('f1', models.PositiveIntegerField()),
],
),
migrations.AddConstraint(
model_name='testcheck',
constraint=models.CheckConstraint(check=models.Q(f1__gte=0),
name='TestCheck_f1_check'),
),
# applied migration:
migrations.AlterField(
model_name='testcheck',
name='f1',
field=models.IntegerField(),
),
}}}
Case 2: trying remove `unique=True` from field when `UniqueConstraint`
exists in `Meta.constraints` with same field will remove both constraints
or raise exception if `UniqueConstraint` has condition:
{{{
class TestUniqCondField(models.Model):
f1 = models.IntegerField(unique=True) # will change to `f1 =
models.IntegerField()`
class Meta:
constraints = [
models.UniqueConstraint(fields=['f1'],
name='TestUniqCondField_f1_uniq', condition=models.Q(f1__gte=0)),
]
# initial migration:
migrations.CreateModel(
name='TestUniqCondField',
fields=[
('id', models.AutoField(auto_created=True,
primary_key=True, serialize=False, verbose_name='ID')),
('f1', models.IntegerField(unique=True)),
],
),
migrations.AddConstraint(
model_name='testuniqcondfield',
constraint=models.UniqueConstraint(condition=models.Q(f1__gte=0),
fields=('f1',), name='TestUniqCondField_f1_uniq'),
),
# applied migration:
migrations.AlterField(
model_name='testuniqcondfield',
name='f1',
field=models.IntegerField(),
),
}}}
Case 3: trying remove `Meta.unique_together` for same
`UniqueConstraint.fields` in `Meta.constraints` get incorrect state via
introspection (the same if UniqueConstraint has condition):
{{{
class TestUniqTogether(models.Model):
f1 = models.IntegerField()
f2 = models.IntegerField()
class Meta:
unique_together = [
('f1', 'f2'), # will be removed
]
constraints = [
models.UniqueConstraint(fields=['f1', 'f2'],
name='TestUniqTogether_f1_f2_uniq'),
]
# initial migration:
migrations.CreateModel(
name='TestUniqTogether',
fields=[
('id', models.AutoField(auto_created=True,
primary_key=True, serialize=False, verbose_name='ID')),
('f1', models.IntegerField()),
('f2', models.IntegerField()),
],
),
migrations.AddConstraint(
model_name='testuniqtogether',
constraint=models.UniqueConstraint(fields=('f1', 'f2'),
name='TestUniqTogether_f1_f2_uniq'),
),
migrations.AlterUniqueTogether(
name='testuniqtogether',
unique_together={('f1', 'f2')},
),
# applied migration
migrations.AlterUniqueTogether(
name='testuniqtogether',
unique_together=set(),
),
}}}
Case 4: trying remove `Meta.index_together` for same `Index.fields` in
`Meta.indexes` get incorrect state via introspection (the same if Index
has condition):
{{{
class TestIndexTogether(models.Model):
f1 = models.IntegerField()
f2 = models.IntegerField()
class Meta:
index_together = [
('f1', 'f2'), # will be removed
]
indexes = [
models.Index(fields=['f1', 'f2'],
name='TestIndexTogether_f1_f2_index'),
]
# initial migration:
migrations.CreateModel(
name='TestIndexTogether',
fields=[
('id', models.AutoField(auto_created=True,
primary_key=True, serialize=False, verbose_name='ID')),
('f1', models.IntegerField()),
('f2', models.IntegerField()),
],
),
migrations.AddIndex(
model_name='testindextogether',
index=models.Index(fields=['f1', 'f2'],
name='TestIndexTogether_f1_f2_index'),
),
migrations.AlterIndexTogether(
name='testindextogether',
index_together={('f1', 'f2')},
),
# applied migration:
migrations.AlterIndexTogether(
name='testindextogether',
index_together=set(),
),
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/30172>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
Ticket URL: <https://code.djangoproject.com/ticket/30172#comment:1>
* owner: nobody => Pavel Tyslacki
* status: new => assigned
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/30172#comment:2>
* needs_better_patch: 0 => 1
* stage: Unreviewed => Accepted
Comment:
Can you look into the SQLite failures?
--
Ticket URL: <https://code.djangoproject.com/ticket/30172#comment:3>
Comment (by Pavel Tyslacki):
Replying to [comment:3 Tim Graham]:
> Can you look into the SQLite failures?
Yep. Hope will fix soon.
--
Ticket URL: <https://code.djangoproject.com/ticket/30172#comment:4>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/30172#comment:5>
* severity: Normal => Release blocker
Comment:
Marking as a release blocker since it's a bug in a new feature
(`Meta.constraints`).
--
Ticket URL: <https://code.djangoproject.com/ticket/30172#comment:6>
Comment (by Tim Graham <timograham@…>):
In [changeset:"4bb859e24694f6cb8974ed9d2225f18214338ea3" 4bb859e]:
{{{
#!CommitTicketReference repository=""
revision="4bb859e24694f6cb8974ed9d2225f18214338ea3"
Refs #30172 -- Prevented removing a field's check or unique constraint
from removing Meta constraints.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/30172#comment:7>
Comment (by Tim Graham <timograham@…>):
In [changeset:"5c17c273ae2d7274f1fa78218b3b74690efddb86" 5c17c273]:
{{{
#!CommitTicketReference repository=""
revision="5c17c273ae2d7274f1fa78218b3b74690efddb86"
Refs #30172 -- Prevented removing a model Meta's index/unique_together
from removing Meta constraints/indexes.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/30172#comment:8>
Comment (by Tim Graham <timograham@…>):
In [changeset:"3dd5e71752c993df00e01fca8086a1af5ba89176" 3dd5e717]:
{{{
#!CommitTicketReference repository=""
revision="3dd5e71752c993df00e01fca8086a1af5ba89176"
[2.2.x] Refs #30172 -- Prevented removing a field's check or unique
constraint from removing Meta constraints.
Backport of 4bb859e24694f6cb8974ed9d2225f18214338ea3 from master.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/30172#comment:9>
Comment (by Tim Graham <timograham@…>):
In [changeset:"2a92e2e3c12e5c4cad0ce2a2d5964675ef837fe7" 2a92e2e]:
{{{
#!CommitTicketReference repository=""
revision="2a92e2e3c12e5c4cad0ce2a2d5964675ef837fe7"
[2.2.x] Refs #30172 -- Prevented removing a model Meta's
index/unique_together from removing Meta constraints/indexes.
Backport of 5c17c273ae2d7274f1fa78218b3b74690efddb86 from master.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/30172#comment:10>
* status: assigned => closed
* resolution: => fixed
--
Ticket URL: <https://code.djangoproject.com/ticket/30172#comment:11>