Opened 9 years ago
Closed 9 years ago
#26468 closed Bug (needsinfo)
Bug in models.functions.ConcatPair for sqlite
Reported by: | jerch | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.8 |
Severity: | Normal | Keywords: | sqlite concat |
Cc: | josh.smeaton@… | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Concat is not working for me due to this line in models.functions.ConcatPair.coalesce:
Coalesce(expression, Value('')) for expression in c.get_source_expressions()
With a minor change it works as intended:
Coalesce(expression, Value('""')) for expression in c.get_source_expressions()
Tested with Django 1.8.12 under Ubuntu with sqlite 3.8.2-1ubuntu2.1.
Change History (7)
comment:1 by , 9 years ago
comment:2 by , 9 years ago
I tried to concat two db fields into one output column like this:
expr = Concat(models.F('field1'), models.Value('"-"'), models.F('field2'), output_field=models.CharField())
Expected output: 'value_from_field_1-value_from_field_2'
Instead I just got an invalid syntax sql error:
django.db.utils.OperationalError: near ")": syntax error
Problem is that the empty Value('')
creates something like this for sqlite:
COALESCE("A", )
which is not valid for sqlite:
sqlite> SELECT COALESCE("A", ); Error: near ")": syntax error sqlite> SELECT COALESCE("A", ""); A
while the second with an empty string with string delimiters is.
comment:3 by , 9 years ago
Component: | Uncategorized → Database layer (models, ORM) |
---|---|
Keywords: | sqlite concat added |
comment:4 by , 9 years ago
I can't reproduce:
In [6]: from django.db import connection In [7]: connection.vendor Out[7]: u'sqlite' In [8]: concat = Concat(F('name'), Value('-'), F('motto'), output_field=CharField()) In [9]: Company.objects.annotate(concat=concat).first().concat Out[9]: u'HI-' In [10]: concat = Concat(F('name'), Value('"-"'), F('motto'), output_field=CharField()) In [11]: Company.objects.annotate(concat=concat).first().concat Out[11]: u'HI"-"' In [12]: import django In [13]: django.get_version() Out[13]: '1.10.dev20160401110429'
Note that I tried both Value('"-"') and Value('-'). You shouldn't be injecting quotes into Value, it'll take care of itself.
Can you please post the full queryset you're running that generates the error, and also print the underlying query of the queryset, like this:
qs = Model.objects.whatever() print(qs.query)
Also, what version of Django are you using? Can you reproduce on master?
comment:5 by , 9 years ago
Cc: | added |
---|
comment:7 by , 9 years ago
Resolution: | → needsinfo |
---|---|
Status: | new → closed |
Please reopen if you can provide more details about your Django, Python and SQLite3 version (import sqlite3; sqlite3.version
).
When you say "not working for me" -- what did you give as inputs, what did you expect to happen and what happened instead?