#30621 closed Bug (fixed)
CheckConstraint crash with __contains lookup on DateTimeRangeFields.
Reported by: | Tilman Koschnick | Owned by: | Mariusz Felisiak |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 2.2 |
Severity: | Release blocker | Keywords: | |
Cc: | Ian Foote | 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
I have a CheckConstraint:
CheckConstraint(check=m.Q(outer__contains=m.F('inner')), name='outer_contains_inner')
where both outer and inner are DateTimeRangeFields. During migration, the inner range gets cast to its underlying type:
ALTER TABLE "test" ADD CONSTRAINT "outer_contains_inner" CHECK ("outer" @> ("inner")::timestamp with time zone)
The attached patch avoids the cast if both ranges are of compatible type (have not checked what happens if one is DateRange and the other is DateTimeRange, though).
Another workaround would be to switch the relation around:
CheckConstraint(check=m.Q(inner__contained_by=m.F('outer')), name='inner_contained_by_outer')
but I feel since both give equal results, they should both be possible.
Attachments (1)
Change History (12)
by , 5 years ago
Attachment: | ranges.diff added |
---|
comment:1 by , 5 years ago
Cc: | added |
---|---|
Has patch: | unset |
Severity: | Normal → Release blocker |
Summary: | CheckConstraint: outer range contains inner range casts rhs field to lhs.inner_type → CheckConstraint crash with __contains lookup on DateTimeRangeFields. |
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 5 years ago
To be honest, I am not quite sure why and when you would cast the expression in the first place. Shouldn't the programmer make sure to pass the right kind of value?
follow-up: 4 comment:3 by , 5 years ago
For me it is a bug and you're patch looks reasonable at first glance.
comment:4 by , 5 years ago
Replying to felixxm:
For me it is a bug and you're patch looks reasonable at first glance.
It's a bug for sure - but I believe a better fix would be to drop the casting bit entirely, but I don't know about the reasoning for putting it in in the first place, so would be reluctant to remove it.
comment:5 by , 5 years ago
You can check #26903 and 6b048b364ca1e0e56a0d3815bf2be33ac9998355 for the reasoning.
comment:6 by , 5 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:8 by , 5 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Thanks for the report. Would you like to submit a patch (with release notes and tests) via GitHub?
Reproduced at 34a88b21dae71a26a9b136b599e5cbcf817eae16.