#30494: Problem with ExtractYear()+1 in queries
-------------------------------------+-------------------------------------
Reporter: Alexey | Owner: nobody
Chernov |
Type: Bug | Status: new
Component: Database | Version: 2.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
It looks like django doesn't create a correct MySQL query when having a
simple arithmetics with ExtractYear().
I have a model with two data fields:
{{{
class Event(models.Model):
start_date = models.DateField()
finish_date = models.DateField()
}}}
If I want to find all rows such that the years of start_date and
finish_date are equal, I can do it so:
{{{
from django.db.models import F
from django.db.models.functions import ExtractYear
>>> print
Event.objects.annotate(year=ExtractYear(F('start_date'))).filter(finish_date__year=F('year')).only('pk').query
SELECT `event`.`id`, EXTRACT(YEAR FROM `event`.`start_date`) AS `year`
FROM `event` WHERE EXTRACT(YEAR FROM `event`.`finish_date`) =
(EXTRACT(YEAR FROM `event`.`start_date`))
}}}
But if I want to find events that start and finish in consequent years, I
try the following filter, and it gives me an incorrect MySQL query:
{{{
>>> print
Event.objects.annotate(year=ExtractYear(F('start_date'))).filter(finish_date__year=F('year')+1).only('pk').query
SELECT `event`.`id`, EXTRACT(YEAR FROM `event`.`start_date`) AS `year`
FROM `event` WHERE `event`.`finish_date` BETWEEN 0001-01-01 AND 0001-12-31
}}}
I tried to replace `F('year')+1` with `F('year')+Value(1)` but it didn't
help.
Am I wrong somewhere, or does it look like a bug?
--
Ticket URL: <https://code.djangoproject.com/ticket/30494>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/055.e877e109ba6319d78532c65a5bf52a9d%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.