Opened 2 years ago
Closed 2 years ago
#34744 closed Bug (fixed)
Migration re-add constraints when check condition contains a dict_keys object.
| Reported by: | bcail | Owned by: | David Sanders |
|---|---|---|---|
| Component: | Migrations | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Adam Zapletal, David Sanders | Triage Stage: | Ready for checkin |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
A CheckConstraint using dict_keys like this has an issue:
models.CheckConstraint(
name='animal_values',
check=models.Q(animal__in={'c': 'cat', 'd': 'dog'}.keys())
)
It continually creates migrations and prompts you to create another migration - it doesn't realize that the migration has already been generated and applied.
If I change it to a list like this, it works - the problem goes away:
models.CheckConstraint(
name='animal_values',
check=models.Q(animal__in=list({'c': 'cat', 'd': 'dog'}.keys()))
)
Should Django properly handle dict_keys, or should the docs say that they have to be converted to a list?
Change History (7)
comment:1 by , 2 years ago
| Cc: | added |
|---|
comment:2 by , 2 years ago
| Cc: | added |
|---|
comment:3 by , 2 years ago
| Summary: | Migrations with dict_keys CheckConstraint → Migration re-add constraints when check condition contains a dict_keys object. |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
| Type: | Uncategorized → Bug |
I think it's worth fixing (check out a similar ticket #30350).
comment:4 by , 2 years ago
Thanks David and Mariusz.
I added print statements to MigrationAutodetector.create_altered_constraints, and that confirms the serialized vs non-serialized states:
old_constraints=[<CheckConstraint: check=(AND: ('animal__in', ('c', 'd'))) name='animal_values'>]
new_constraints=[<CheckConstraint: check=(AND: ('animal__in', dict_keys(['c', 'd']))) name='animal_values'>]
I'm not sure how to best fix this, though.
comment:5 by , 2 years ago
I have a draft PR which serialises during the change detection: https://github.com/django/django/pull/17114
I'm not particularly fond of this though. For one thing this probably would've masked the real issue behind #30350? 🤔 It also only narrowly focuses on one part of the project state.
comment:6 by , 2 years ago
| Has patch: | set |
|---|---|
| Owner: | changed from to |
| Status: | new → assigned |
| Triage Stage: | Accepted → Ready for checkin |
Without offering opinion on whether this is a bug or not, here are some notes:
So we have a situation where migrations is comparing serialized state to pre-serialized state.
While this could potentially be a bug, the workaround is to simply use a list and "fixing" this issue could break expected current behaviour? 🤔