[Django] #32337: Django Check Constraint is not enforced at all

4 views
Skip to first unread message

Django

unread,
Jan 9, 2021, 11:13:15 PM1/9/21
to django-...@googlegroups.com
#32337: Django Check Constraint is not enforced at all
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
CosmicReindeer |
Type: Bug | Status: new
Component: Database | Version: 3.1
layer (models, ORM) |
Severity: Normal | Keywords: check constraint
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 model like this with the following structure.
{{{
class Role(BaseModel):
class Meta:
verbose_name = 'role'
verbose_name_plural = 'roles'
ordering = ['position', 'cluster']
required_db_features = {
'supports_deferrable_unique_constraints',
}
constraints = [
models.UniqueConstraint(
fields=['position', 'cluster'],
name='deferrable_unique_role_position',
deferrable=models.Deferrable.DEFERRED
),
models.CheckConstraint(
name='default_role_check',
check=(
models.Q(is_default=True, position=1, color='#969696',
name='@everyone') |
models.Q(is_default=False)
)
),
]

permission_flags = [
'READ_DATA', 'WRITE_DATA', 'MANAGE_RECORDS', 'MANAGE_ROLES',
'MANAGE_CLUSTER', 'MANAGE_DATASHEETS', 'MANAGE_FIELDS',
'MANAGE_CONSTRAINTS',
'KICK_MEMBERS', 'MANAGE_MEMBERS',
]

default_perm_flags = ['READ_DATA', 'WRITE_DATA', 'MANAGE_RECORDS']

def __str__(self):
return self.name

objects = managers.RolesManager()
positions = managers.PositionalManager()
permissions = BitField(flags=permission_flags,
default=default_perm_flags, db_index=True)
position = models.PositiveSmallIntegerField(null=True, blank=True,
db_index=True, editable=True)
name = models.CharField(max_length=100,
validators=[MinLengthValidator(2)], db_index=True, default='new role')
color = ColorField(db_index=True, default='#969696')
is_default = models.BooleanField(default=False, db_index=True)
cluster = models.ForeignKey('api_backend.Cluster',
on_delete=models.CASCADE, editable=False)

REQUIRED_FIELDS = [name, cluster]
}}}
Basically I want to ensure that a role with `is_default` set to True
should always have a position of 1 and name of '@everyone' and a color of
'#969696'

However, I am able to edit the model through serializers for the same, and
the name changes to something other than '@everyone'. No errors are
raised. Same with the position.
The constraint isnt enforced for updates or bulkupdates, too.

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

Django

unread,
Jan 9, 2021, 11:25:33 PM1/9/21
to django-...@googlegroups.com
#32337: Django Check Constraint is not enforced at all
-------------------------------------+-------------------------------------
Reporter: Aryan Iyappan | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: check constraint | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Aryan Iyappan):

* status: new => closed
* resolution: => fixed
* stage: Unreviewed => Accepted


Comment:

The problem was that the table was altered when there was existing data in
the database and the constraint was added. I cleared my database and now,
the constraint works properly.

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

Django

unread,
Jan 10, 2021, 5:38:26 PM1/10/21
to django-...@googlegroups.com
#32337: Django Check Constraint is not enforced at all
-------------------------------------+-------------------------------------
Reporter: Aryan Iyappan | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: check constraint | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* resolution: fixed => invalid


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

Django

unread,
Jan 11, 2021, 12:34:23 AM1/11/21
to django-...@googlegroups.com
#32337: Django Check Constraint is not enforced at all
-------------------------------------+-------------------------------------
Reporter: Aryan Iyappan | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: check constraint | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Unreviewed


--
Ticket URL: <https://code.djangoproject.com/ticket/32337#comment:3>

Reply all
Reply to author
Forward
0 new messages