[Django] #32418: Django f expressions seems to not work with MySQL

57 views
Skip to first unread message

Django

unread,
Feb 4, 2021, 6:37:08 AM2/4/21
to django-...@googlegroups.com
#32418: Django f expressions seems to not work with MySQL
-----------------------------------------+------------------------
Reporter: HoshiYamazaki | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 3.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
My code:
```python
duration_expression = ExpressionWrapper(
datetime.timedelta(minutes=1) * F('duration'),
output_field=DurationField())
booking_end = ExpressionWrapper(
F('booking_dt') + duration_expression,
output_field=DateTimeField())
booking_qs.annotate(
booking_end=booking_end).get(
Q(Q(booking_dt__lte=first_interval),
Q(booking_dt__gte=next_interval)) |
Q(Q(booking_end__lte=first_interval),
Q(booking_end__gte=next_interval)))duration_expression =
ExpressionWrapper(
datetime.timedelta(minutes=1) * F('duration'),
output_field=DurationField())
booking_end = ExpressionWrapper(
F('booking_dt') + duration_expression,
output_field=DateTimeField())
booking_qs.annotate(
booking_end=booking_end).get(
Q(Q(booking_dt__lte=first_interval),
Q(booking_dt__gte=next_interval)) |
Q(Q(booking_end__lte=first_interval),
Q(booking_end__gte=next_interval)))
```

Error I get:
```
(1064, "You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near
'* `carwashes_carwashbooking`.`duration`) MICROSECOND) AS `booking_end`
FROM `car' at line 1")
```

SQL query:
```sql
('SELECT `carwashes_carwashbooking`.`id`, '
'`carwashes_carwashbooking`.`carwash_id`, '
'`carwashes_carwashbooking`.`created_dt`, '
'`carwashes_carwashbooking`.`booking_dt`, '
'`carwashes_carwashbooking`.`duration`,
`carwashes_carwashbooking`.`car_id`, '
'`carwashes_carwashbooking`.`car_name`,
`carwashes_carwashbooking`.`plate`, '
'`carwashes_carwashbooking`.`status`,
`carwashes_carwashbooking`.`driver_id`, '
'`carwashes_carwashbooking`.`phone_number`, '
'`carwashes_carwashbooking`.`service_id`,
`carwashes_carwashbooking`.`notes`, '
'(`carwashes_carwashbooking`.`booking_dt` + INTERVAL (INTERVAL 60000000 '
'MICROSECOND * `carwashes_carwashbooking`.`duration`) MICROSECOND) AS '
'`booking_end` FROM `carwashes_carwashbooking` WHERE '
'(`carwashes_carwashbooking`.`carwash_id` = %s AND '
'`carwashes_carwashbooking`.`driver_id` = %s AND '
'((`carwashes_carwashbooking`.`booking_dt` <= %s AND '
'`carwashes_carwashbooking`.`booking_dt` >= %s) OR '
'((`carwashes_carwashbooking`.`booking_dt` + INTERVAL (INTERVAL 60000000
'
'MICROSECOND * `carwashes_carwashbooking`.`duration`) MICROSECOND) <= %s
AND '
'(`carwashes_carwashbooking`.`booking_dt` + INTERVAL (INTERVAL 60000000 '
'MICROSECOND * `carwashes_carwashbooking`.`duration`) MICROSECOND) >=
%s))) '
'LIMIT 21')
```

Stackoverflow issue: https://stackoverflow.com/questions/66044255/django-
returns-sql-syntax-error-when-using-expressionwrapper-and-f-expressions

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

Django

unread,
Feb 4, 2021, 6:38:12 AM2/4/21
to django-...@googlegroups.com
#32418: Django f expressions seems to not work with MySQL
-------------------------------+--------------------------------------

Reporter: HoshiYamazaki | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 3.1
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by HoshiYamazaki:

Old description:

New description:

My code:


{{{

Error I get:


SQL query:


{{{

--

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

Django

unread,
Feb 4, 2021, 6:38:46 AM2/4/21
to django-...@googlegroups.com
#32418: Django f expressions seems to not work with MySQL due to some
interval/duration error
-------------------------------+--------------------------------------

Reporter: HoshiYamazaki | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 3.1
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

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

Django

unread,
Feb 4, 2021, 9:50:34 AM2/4/21
to django-...@googlegroups.com
#32418: F() expressions crash due to some interval/duration error on MySQL.
-------------------------------------+-------------------------------------
Reporter: Hoshi Yamazaki | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution: duplicate

Keywords: | 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):

* status: new => closed
* resolution: => duplicate
* component: Uncategorized => Database layer (models, ORM)


Old description:

> My code:
>

> {{{

>
> Error I get:
>

> SQL query:
>

> {{{

New description:

My code:


{{{


duration_expression = ExpressionWrapper(
datetime.timedelta(minutes=1) * F('duration'),
output_field=DurationField())
booking_end = ExpressionWrapper(
F('booking_dt') + duration_expression,
output_field=DateTimeField())
booking_qs.annotate(
booking_end=booking_end).get(
Q(Q(booking_dt__lte=first_interval),
Q(booking_dt__gte=next_interval)) |
Q(Q(booking_end__lte=first_interval),

Q(booking_end__gte=next_interval)))
}}}

Error I get:

{{{
(1064, "You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near
'* `carwashes_carwashbooking`.`duration`) MICROSECOND) AS `booking_end`
FROM `car' at line 1")
}}}


SQL query:


{{{

--

Comment:

Duplicate of #28925, fixed in dd5aa8cb5ffc0a89c4b9b8dee45c1c919d203489
(Django 3.2+).

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

Reply all
Reply to author
Forward
0 new messages