[Django] #28371: Cast generating invalid SQL for SQLite and PostgreSQL

27 views
Skip to first unread message

Django

unread,
Jul 6, 2017, 5:26:23 PM7/6/17
to django-...@googlegroups.com
#28371: Cast generating invalid SQL for SQLite and PostgreSQL
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
jamesdoherty |
Type: Bug | Status: new
Component: Database | Version: 1.11
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 |
-------------------------------------+-------------------------------------
I have a nullable IntegerField. When trying to cast it to a char, SQLite
and PostgreSQL error due to invalid SQL:

{{{
class Numbers(models.Model):
number = models.IntegerField(null=True)
}}}

The following code causes the error.
{{{
Numbers.objects.annotate(as_string=Cast('number', CharField()))
}}}

PostgreSQL

{{{
ProgrammingError: syntax error at or near "None"
LINE 1: ...mbers"."number", "demo_numbers"."number"::varchar(None) AS "...

SELECT "demo_numbers"."id", "demo_numbers"."number",
"demo_numbers"."number"::varchar(None) AS "as_string" FROM "demo_numbers"
}}}

Removing the '(None)' from the SQL makes this work.

SQLite

{{{
OperationalError: near "None": syntax error

SELECT "demo_numbers"."id", "demo_numbers"."number",
CAST("demo_numbers"."number" AS varchar(None)) AS "as_string" FROM
"demo_numbers"
}}}

According to the SQLite documentation, varchar is not a valid type for
SQLite: http://www.sqlite.org/lang_expr.html#castexpr

Changing the SQL to 'CAST("demo_numbers"."number" AS TEXT)' succeeds. It's
worth noting that it is possible to give SQLite an invalid cast type that
doesn't cause an error (eg, try 'CAST("demo_numbers"."number" AS BOGUS)' )

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

Django

unread,
Jul 7, 2017, 6:39:54 AM7/7/17
to django-...@googlegroups.com
#28371: Cast generating invalid SQL for SQLite and PostgreSQL
-------------------------------------+-------------------------------------
Reporter: jamesdoherty | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) |
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
-------------------------------------+-------------------------------------
Changes (by felixxm):

* cc: felixxm (added)
* version: 1.11 => master


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

Django

unread,
Jul 8, 2017, 8:30:36 AM7/8/17
to django-...@googlegroups.com
#28371: Cast generating invalid SQL for SQLite and PostgreSQL
-------------------------------------+-------------------------------------
Reporter: jamesdoherty | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: master
(models, ORM) |
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
-------------------------------------+-------------------------------------

Comment (by felixxm):

I confirm that if you use `Cast` with `CharField()` without `max_length`
argument then SQL is invalid. On the other hand I'm not convinced that
it's a bug because `max_length` is required for `CharField`'s.

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

Django

unread,
Jul 10, 2017, 12:18:44 PM7/10/17
to django-...@googlegroups.com
#28371: Cast generates invalid SQL for SQLite and PostgreSQL if max_length isn't
provide to CharField

-------------------------------------+-------------------------------------
Reporter: jamesdoherty | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Unreviewed => Accepted


Comment:

For `Cast`, I'm not sure if `max_length` matters. If not too complicated,
I think it wouldn't be unreasonable to allow this to work -- or at least a
more helpful error message should be provided.

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

Django

unread,
Jul 12, 2017, 2:00:41 PM7/12/17
to django-...@googlegroups.com
#28371: Cast generates invalid SQL for SQLite and PostgreSQL if max_length isn't
provide to CharField
-------------------------------------+-------------------------------------
Reporter: James Doherty | Owner: felixxm
Type: Bug | Status: assigned

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: new => assigned
* owner: nobody => felixxm


--
Ticket URL: <https://code.djangoproject.com/ticket/28371#comment:4>

Django

unread,
Jul 12, 2017, 2:25:08 PM7/12/17
to django-...@googlegroups.com
#28371: Cast generates invalid SQL for SQLite, PostgreSQL, and Oracle if max_length

isn't provide to CharField
-------------------------------------+-------------------------------------
Reporter: James Doherty | Owner: felixxm
Type: Bug | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
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/28371#comment:5>

Django

unread,
Jul 13, 2017, 4:26:06 AM7/13/17
to django-...@googlegroups.com
#28371: Cast generates invalid SQL for SQLite, PostgreSQL, and Oracle if max_length
isn't provide to CharField
-------------------------------------+-------------------------------------
Reporter: James Doherty | Owner: felixxm
Type: Bug | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/8758 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/28371#comment:6>

Django

unread,
Jul 26, 2017, 8:27:14 PM7/26/17
to django-...@googlegroups.com
#28371: Cast generates invalid SQL for SQLite, PostgreSQL, and Oracle if max_length
isn't provide to CharField
-------------------------------------+-------------------------------------
Reporter: James Doherty | Owner: felixxm
Type: Bug | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"8e41373c81cbd914d84b362ca4c85d3ed4fcff43" 8e41373]:
{{{
#!CommitTicketReference repository=""
revision="8e41373c81cbd914d84b362ca4c85d3ed4fcff43"
Allowed database backends to specify data types for Cast().

A small refactor ahead of refs #28371.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28371#comment:7>

Django

unread,
Jul 27, 2017, 1:37:03 PM7/27/17
to django-...@googlegroups.com
#28371: Cast generates invalid SQL for SQLite, PostgreSQL, and Oracle if max_length
isn't provide to CharField
-------------------------------------+-------------------------------------
Reporter: James Doherty | Owner: felixxm
Type: Bug | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by GitHub <noreply@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"b61d5b1991e2ca2c3450ccc334224f3d51da39dc" b61d5b1]:
{{{
#!CommitTicketReference repository=""
revision="b61d5b1991e2ca2c3450ccc334224f3d51da39dc"
Fixed #28371 -- Fixed Cast() with CharField if the max_length argument
isn't provided.

Thanks Tim Graham for the review.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28371#comment:8>

Reply all
Reply to author
Forward
0 new messages