﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
30621	CheckConstraint crash with __contains lookup on DateTimeRangeFields.	Tilman Koschnick	Mariusz Felisiak	"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."	Bug	closed	Database layer (models, ORM)	2.2	Release blocker	fixed		Ian Foote	Ready for checkin	1	0	0	0	0	0
